PHP Reflection with two calls

只愿长相守 提交于 2019-12-25 09:16:27

问题


I am trying to archive some reflection in php. How can i make something like that. With my code i am getting following error:

Undefined property: A::$getB()->getStr()

class B{
    public function getStr(){
        return 'str';
    }
}
class A{
    public function getB(){
        return new B();
    }
}

$a = new A();
$method = 'getB()->getStr()';
echo($a->$method);

回答1:


You have to split the call chain to the single calls

$getB = "getB";
$str = "getStr";
$a = new A();
echo $a->$getB()->$str();



回答2:


You should definitely use ReflectionClass and ReflectionMethod instead of string wizardy.



来源:https://stackoverflow.com/questions/41062136/php-reflection-with-two-calls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!