两次调用的 PHP 反射 [英] PHP Reflection with two calls

查看:34
本文介绍了两次调用的 PHP 反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 php 中存档一些反射.我怎么能做出那样的东西.使用我的代码,我收到以下错误:

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

未定义的属性:A::$getB()->getStr()

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);

推荐答案

您必须将调用链拆分为单个调用

You have to split the call chain to the single calls

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

这篇关于两次调用的 PHP 反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆