値が初期化される前に値にアクセスしています。まず、値を設定する必要があります。
例:メソッドは、子クラスのコンストラクタで値を設定する親クラスです。
class Child extends Parent
{
public function __construct() {
$this -> setConfig(); //call some parent method to set the config first
}
public function index()
{
print_r($this->config); // returns an empty array
}
}
更新:また、あなたは親のメソッドとプロパティあなたはどうなるだけで同じように仕事をしなければならない、OOP
class Parent { ..... }
class child extends Parent { ..... }
$p = new Parent(); // will contain all method and properties of parent class only
$c = new Child(); // will contain all method and properties of child class and parent class
の概念について混乱しているように見える。しかし通常のオブジェクト。
class Parent {
protected $config = "config";
}
class Child extends Parent {
public function index() {
echo $this -> config; // THis will successfully echo "config" from the parent class
}
}
しかし、別の例
class Parent {
protected $config;
}
class Child extends Parent {
public function index() {
echo $this -> config; //It call upon the parent's $config, but so far there has been no attempt to set an values on it, so it will give empty output.
}
}
'app'クラスはどのようなものです:
は別の例を見ることができますか? –
アプリクラスその子クラス – Eli