私は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);
}
}
です:
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();
}
}
}
私がする必要があるのは、FROM HOMEPAGE Controlこのようなことをしてはいけません。
$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();
誰でも手伝ってもらえますか?
EDIT:これを行うときに私が取得していますエラーは次のとおりです。
Notice: Undefined property: Home::$test
Fatal error: Call to a member function get_all() on a non-object
注:モデル名がtest
で、モデルの方法は、あなたが何get_all()
私はポストジョンで編集しました。私は何を得ているのですか、ありがとうございました。助けてください –
質問を編集して現在のコードを表示してください。 –
私はコードを編集しました。現在のコードは、家のコントローラ –