I'm still learning OOP so this might not even be possible (although I would be surprised if so), I need some help calling another classes method.
For example in ClassA I
have this method:
function getName() { return $this->name; }
now from ClassB
(different file, but in the same directory), I want to call ClassA
's getName()
, how do I do that? I tried to just do an include()
but that does not work.
Thanks!
PHP What is OOP? OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.
//file1.php <?php class ClassA { private $name = 'John'; function getName() { return $this->name; } } ?> //file2.php <?php include ("file1.php"); class ClassB { function __construct() { } function callA() { $classA = new ClassA(); $name = $classA->getName(); echo $name; //Prints John } } $classb = new ClassB(); $classb->callA(); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With