Return class objects from function

I am trying to reutrn a class from a function and access it out. It is not working.
What do I need to do? Thanks in advance.

Here is the code


<?php class classa { public function foo () { return 'bar'; } } class classb { public static function getmodela () { $model = new modela(); return model; } } $m = new classb(); $n = $m->getclassa(); $o = $n->foo(); print $o; ?>

and here is the error I get

Fatal error: Call to a member function foo() on a non-object in C:\php\examples\b.php on line 18

Hi,

you define ‘classa’ and try to instantiate a ‘modela’.
Also the function ‘getclassa()’ isn’t defined, it’s ‘getmodela()’.

So indeed the ‘object’ you try to call ‘foo()’ on is a ‘null’ variable. :wink:

Hope this helps,
O.

Sponsor our Newsletter | Privacy Policy | Terms of Service