Chaining Object Calls in PHP4

I ran across a SitePoint post where a user wanted to chain calls to returned objects in PHP4, similar to what you can now do in PHP5. Of course PHP4 does not allow this syntax, but the user came up with the idea of calling a function with the base object, and the series of calls as a string argument. The function would then parse the string and apropriatly eval() the calls. I took his idea as a springboard, and re-wrote it to use recursion for arbitrary call depth.

Here are the functions:

function &chain(&$obj, $call) {
        return call_chain($obj, explode('->',$call));
function &call_chain(&$obj, $stack) {
        if ($stack) {
                eval('$new_obj =& $obj->'.array_shift($stack).';');
                return call_chain($new_obj, $stack);
        } else {
                return $obj;


And here is a cheesy example:

class chainOne { 
  function &makeTwo() { return new chainTwo; }
class chainTwo { 
  function &makeThree($a,$b) { return new chainThree($b); }
class chainThree { 
  var $x; 
  function chainThree($x) { $this->x = $x; }
  function &makeTest() { return new chainTest($this->x); }
class chainTest { 
  var $y; 
  function chainTest($y) { $this->y = $y; }
  function &doIt($z) { return array('made it!',$this->y,$z); }

$GLOBALS['foo'] = 'baz';
$one =& new chainOne;