Recipe 7.12. Calling Methods on an Object Returned by Another Method


Recipe 7.12. Calling Methods on an Object Returned by Another Method

7.12.1. Problem

You need to call a method on an object returned by another method.

7.12.2. Solution

Call the second method directly from the first:

$orange = $fruit->get('citrus')->peel();

7.12.3. Discussion

PHP is smart enough to first call $fruit->get('citrus') and then invoke the peel( ) method on what's returned.

This is an improvement over PHP 4, where you needed to use a temporary variable:

$orange = $fruit->get('citrus'); $orange->peel();

Another victory for PHP 5!




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

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