Section 8.18. Dereferencing Object Return Values


8.18. Dereferencing Object Return Values

If you call a function that returns an object, you can treat the return value of that function as an object from the calling line and access it directly. For example:

     $lassie = new Dog( );     $collar = $lassie->getCollar( );     echo $collar->Name;     $poppy = new Dog( );     echo $poppy->getCollar( )->Name; 

In the first example, we need to call getCollar( ) and save the returned value into $collar, before echoing out the Name property of $collar. In the second example, we use the return value from getCollar( ) immediately from within the same line of code, and echo out Name without an intermediate property like $collar.

For now at least, return value dereferencing only applies to objects. If you have a function someFunc( ) that returns an array, for example, using $obj->someFunc( )[3] to access an element in the return value will cause a parse erroryou need to store the return value in another property, then access it.




PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net