我遇到了一个奇怪的情况,也许你可以帮助我理解这一点。
$object = array('controller' => 'frontend_shop', 'method' => 'category');
include_once(PATH.'controllers/'.$object['controller'].'.php');
$controller = new $object['controller']($object);
class Frontend_shop extends Controller {
public $controller;
public function __construct($object)
{
// Works
$this->$object['method']();
//Don´t work
$this->controller = $object;
$this->controller['method']();
}
public function category()
{
echo 'hello';
}
}
这有效并显示“你好”
$this->$object['method']();
但是当我将此数组分配给一个类变量时,例如:
$this->controller = $object;
$this->controller['method']();
我得到:
Fatal error: Call to undefined function category() in /usr/lib/app/application/controllers/frontend_shop.php on line 10
当然我知道我可以使用第一种方法,但也许你可以解释一下类变量的方法有什么问题。谢谢