I have the following string:
$str = "methodA()->methodB()->methodC"
And i want to call that “chain” on an object
$obj->$str
I am currently splitting with ()-> and calling one by one, but there must be abetter way.
How can i call it on one line? thanks!
EDIT: Some more context information:
I created a Doctrine Behaviour for symfony 1.4 to be able to make certain tables from the schema searchable with Zend Lucene.
In short, i need a way to say to the behaviour “To get the searchable field you must call methodA()->methodB()->methodC()”
EDIT2:
Maybe i wasnt clear enough. $obj and $str are determined at runtime, so some of your suggestions are not applicable.
EDIT3:
In case you are wondering, i am currently doing this:
<?php
$chain = explode("()->",$str);
$method = array_shift($chain);
$value = $obj->$method();
foreach($chain as $method){
$value = $value->$method();
}
After writing my comment to the main question I thought of a possible solution, though a little verbose and manual (you would have to make as many case statements as you would reasonably expect for the number of chained methods).
Usage would be: