我试图让一个子类(提要)从主类(控制器)继承一个变量。
我有 3 个文件。它们看起来如下。
核心文件:
/* ----------------------------- *
* LOAD THE CONTROLLER CLASS NOW *
* ----------------------------- */
require(CORE_PATH . 'controller.php');
require(CORE_PATH . 'load_controller.php');
控制器类:
<?php
class Controller {
var $sessions, $a;
public function __construct() {
$this->sessions = new Sessions();
$this->a = 'yo';
}
}
饲料类:
class Feeds extends Controller {
public function feeds() {
/* We can load other resources here */
}
function index($val) {
echo $this->a;
}
}
修复:
<?php require(PROTECT);
class Controller {
protected $variable = "Setting the variable right here works.";
public function __construct() {
$this->variable = "Setting it right here will only work for the current object. It's as if variable isn't being set at all when it comes to other classes.";
}
}