Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Call function on returned Object in PHP (https://www.warriorforum.com/programming/84308-call-function-returned-object-php.html)

Sean Kelly 16th May 2009 03:19 AM

Call function on returned Object in PHP
 
Is it possible to do the following?

$result = function1()->function2()->function3();

It's very handy to write code like this, easier to read and shorter too!

The only alternative I see is this:
$first = function1();
$second = $first->function2();
$result = $second->function3();

This is assuming function1 returns an object containing function2 and function2 returns an object containing function3.

So the question remains.. is this possible:
$result = function1()->function2()->function3();

Thanks!
Sean

Mark Brian 16th May 2009 03:34 AM

Re: Call function on returned Object in PHP
 
in PHP 5 yes.

Sean Kelly 16th May 2009 03:36 AM

Re: Call function on returned Object in PHP
 
Example:

In Java this is possible:

int count = new Family().getEldestChild().getNumberOfToes();

In PHP I would assume it would be something like this:

$count = new Family()->getEldestChild()->getNumberOfToes();

but I'm having no luck with it. Google searches didn't help either.

Any ideas if this is possible?

Sean

Sean Kelly 16th May 2009 04:48 AM

Re: Call function on returned Object in PHP
 
Thanks Mark, yes you're right, I needed to upgrade my version of PHP, I was using an old version of WAMP. It's working now.

Beautiful!

Sean


All times are GMT -6. The time now is 08:06 PM.