7.6. Calling Methods on an Object Returned by Another Method

Problem

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

Solution

Assign the object to a temporary variable, and then call the method of that temporary variable:

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

Discussion

This is necessary because a parse error results from:

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

Zend Engine 2 supports direct dereferencing of objects returned from a method so this workaround is no longer necessary.

Get PHP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.