【发布时间】:2021-02-13 14:29:42
【问题描述】:
我有两个 PHP 文件,一个是“Pages.php”,它有 class Pages{},第二个是“Core.php”,它有 class Core{}。我将“Pages.php”包含在“Core 类”的构造函数中,并尝试从“Pages 类”调用名为home() 的公共方法。但它显示以下错误:
未捕获的错误:Core 类的对象无法转换为字符串。
class Core {
protected $currentController = 'Pages';
protected $currentMethod = 'home';
protected $params = [];
public function __construct() {
require_once '../app/controllers/' . $this->currentController . '.php';
$this->currentController = new $this->currentController();
$this->currentController->$this->currentMethod();
}
}
如果我调用这样的方法
$a = $this->currentMethod;
$this->currentController->$a();
它工作正常...
但是我想知道为什么我不能像$this->currentController->$this->currentMethod();这样调用方法?
【问题讨论】: