我想使用OOP PHP构建MVC模式。请阅读帖子的其余部分,以了解我想要的内容。PHP MVC设计问题
这是网页控制器,其延伸的主控制器
class Home extends Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->load->model("test");
$this->test->get_all();
$data = array (
'name' => "Amr",
'age' =>24
);
$this->load->view("home_view",$data);
}
}
主控制器看起来像这样,并延伸loader类:
class Controller extends Loader {
public $load;
function __construct(){
parent::__construct();
$this->load = new Loader();
}
}
其具有问题Loader类看起来像这样
class Loader {
public $Error;
function __construct(){
$this->Error = new Error();
}
public function model($Modelname){
$this->$Modelname = $Modelname;
if (file_exists("models/".$Modelname.".php")){
require_once $Modelname . ".php";
$this->$Modelname = new $Modelname;
}else{
$this->Error->Not_found();
}
}
public function view($Viewname,$data=NULL){
if(is_array($data)){
extract($data);
}
if (file_exists("views/".$Viewname.".php")){
require_once $Viewname . ".php";
}else{
$this->Error->Not_found();
}
}
public function helper($helper) {
if (file_exists("helpers/".$helper.".php")){
require_once $helper . ".php";
$this->$helper = new $helper;
}else{
$this->Error->Not_found();
}
}
}
我需要做的是能够从主页控制ler做这样的事情:
$this->load->model("someModel"); // model name is test
$this->someModel->someMethodInModel(); // the model method is get_all()
// and the same for helper
$this->load->helper("someHelper");
$this->someHelper->someMethodInHelper();
任何人都可以帮助我吗?
编辑:错误这样做时,我得到的是:
Notice: Undefined property: Home::$test
Fatal error: Call to a member function get_all() on a non-object
注:型号名称是test
和模型方法是get_all()
我在帖子上做了编辑约翰,这就是我能得到,非常感谢你的时间,并希望你可以帮助我 –
请编辑问题并显示您当前的代码。 –
我编辑的代码当前代码是即时通讯使用其相同的除了两条线添加到家庭控制器 –