我正在尝试让子类(feeds)从主类(控制器)继承一个变量。
我有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';
}
}
FEEDS CLASS:
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.";
}
}
答案 0 :(得分:0)
除非你使用PHP4,否则你应该真的避免使用var
声明类成员。使用public, private, protected
在您的情况下,因为您使用了var
,您的成员变量$a
应该可以从子类访问。
你有任何错误吗?