In order to optimize my code, how can I write the last two lines of code in one statement?
$v1 = functionName;
$strR = "get".$v1;
echo $obj->$strR();
As I want to get $obj->getfunctionName()
.
You can enclose it in {}
to shorten your code, but this will not make a significant difference (if any at all) in terms of execution speed:
echo $obj->{"get" . $v1}();
This functionality is described in user-contributed comments on the PHP5 OOP introductory page, though I do not consider that official documentation.
Another approach with no optimization:
<?
Echo "\$obj->getfunctionName();";
?>