【发布时间】:2013-04-24 16:05:15
【问题描述】:
我正在做一个基本的 MVC 练习,但我收到了这个错误:
Fatal error: Call to a member function run() on a non-object in Router.php on line 5
我做错了什么?
核心:
<?php
class Core {
protected $router;
protected $controller;
public function run() {
$this->router =& load_class('Router');
$this->controller =& load_class('Controller');
$this->router->run();
}
}
路由器:
class Router extends Core {
public function run() {
echo $this->controller->run();
}
}
控制器:
class Controller extends Core {
public function run() {
return 'controller';
}
}
哦,还有 load_class 函数
function &load_class($class_name) {
$path = ROOT . 'system/classes/' . $class_name . '.php';
if (file_exists($path)) {
include_once($path);
if (class_exists($class_name)) {
$instance = new $class_name;
return $instance;
}
}
return false;
}
非常感谢。
【问题讨论】:
-
你是如何运行代码的?
-
var_dump($this->controller)显示什么? -
@Barmar 它什么也没显示。
-
@Raidenace 我正在调用 $core->run()
-
错误发生在哪一行?
标签: php oop class visibility siblings