Sanjoy Ganguly - 2009-06-30 05:59:46 -
In reply to message 3 from Corey W.
Hi,
Thanks for your quick reply.You are absolutely right about method chaining. I think you can chain the objects in PHP4 using the following ( You can try this ):-
<?php
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:
<?php
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;
var_dump(
chain($one,
'makeTwo()->makeThree("foo","bar")->makeTest()->doIt($GLOBALS["foo"])'
));
?>
Many Thanks :)