私は2つのクラスを1つ(コントローラクラス)を別のクラスから継承し、コントローラクラスで変数 "load"私は別のクラスから私はコンストラクタ、任意のアイデアからこの変数を呼び出すことはできません拡張するとき? (私の悪い英語に対する謝罪)。別のクラスから継承するときのクラスのコンストラクタの問題私はコンストラクタのメソッドを使用できません
クラスコントローラー:
<?php
class Controller {
protected $load;
public function __construct() {
$this->load = new Loader();
if($_GET && isset($_GET['action']))
{
$action = $_GET['action'];
if(method_exists($this, $action))
$this->$action();
else
die('Method not found.');
} else {
if(method_exists($this, 'index'))
$this->index();
else
die('Index method not found.');
}
}
}
クラスのホーム(それが拡張しない):
<?php
class Home extends Controller
{
function __construct() {
parent::__construct();
$this->load->model("HomeModel");// this line doesn't work
}
public function index() {
$articles = new HomeModel();
$articles = $articles->getData();
$nombres = ['jona', 'juan', 'jose'];
$view = new Views('home/home', compact("nombres", "articles"));
}
}
ローダークラス:
<?php
class Loader
{
function __construct() {
}
public function model($model) {
require('./models/'.$model.'.php');
}
}
何が問題なのですか?どのようなエラーが出ますか? –
@AlexHowanskyそれはちょうど動作しません、私はその行を宣言したことがないのです –
@AlexHowansky "$ this-> load-> model(" HomeModel ");この構造体では、これはうまくいきませんが、私がインデックス関数でこれを正しく宣言したときに、正しく動作します –