Closed Thread
Thread Tools Search this Thread
Unread 16th May 2009, 03:19 AM   #1
HyperActive Warrior
War Room Member
 
Sean Kelly's Avatar
 
Join Date: 2006
Location: Ireland
Posts: 294
Thanks: 5
Thanked 34 Times in 25 Posts
Default
Call function on returned Object in PHP
Share on: 
fb share twitter share gplus share more share

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

http://javadocs.com - Javadocs
Sean Kelly is offline  
Unread 16th May 2009, 03:34 AM   #2
Spartan Warrior
War Room Member
 
Join Date: 2008
Posts: 869
Thanks: 130
Thanked 149 Times in 75 Posts
Default
Re: Call function on returned Object in PHP
Share on: 
fb share twitter share gplus share more share

in PHP 5 yes.


Mark Brian is offline  
Unread 16th May 2009, 03:36 AM   #3
HyperActive Warrior
War Room Member
 
Sean Kelly's Avatar
 
Join Date: 2006
Location: Ireland
Posts: 294
Thanks: 5
Thanked 34 Times in 25 Posts
Default
Re: Call function on returned Object in PHP
Share on: 
fb share twitter share gplus share more share

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

http://javadocs.com - Javadocs
Sean Kelly is offline  
Unread 16th May 2009, 04:48 AM   #4
HyperActive Warrior
War Room Member
 
Sean Kelly's Avatar
 
Join Date: 2006
Location: Ireland
Posts: 294
Thanks: 5
Thanked 34 Times in 25 Posts
Default
Re: Call function on returned Object in PHP
Share on: 
fb share twitter share gplus share more share

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

http://javadocs.com - Javadocs
Sean Kelly is offline  
Closed Thread


Bookmarks

Tags
call, function, object, php, returned

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




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