我有两个类,一个(控制器类)从另一个扩展,然后在控制器类中,我定义一个变量" 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.');
}
}
}
Class home(它在哪里延伸):
<?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');
}
}
答案 0 :(得分:0)
错误&#34;&#39; HomeModel&#39;找不到&#34;会让我相信你不需要包含&#39; HomeModel&#39; “家庭”课程中的课程类文件。