PHP Reflection with two calls

2019-09-14 23: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);

2条回答
\"骚年 ilove
2楼-- · 2019-09-15 00:05

You have to split the call chain to the single calls

$getB = "getB";
$str = "getStr";
$a = new A();
echo $a->$getB()->$str();
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-09-15 00:09

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

查看更多
登录 后发表回答