わかりました、少し問題があります。シナリオは次のとおりです。test2 のコンストラクターを取得して、main_class のコンストラクターによって設定された main_class 内にあるクラス プロパティ test にアクセスできるようにする必要があります。それを機能させる方法がわかりません。システムがまさにこのように機能する必要があります。さて、クラス定義でこのようにコードでクラス変数を設定すると、これは機能var test = "hello";
しますが、もちろんこの場合、main_class::test はコンストラクターによって設定され、「var」ではないため、機能しません。動作しません。
これが私のコードの非常に単純化されたバージョンです:
index.php:
<?php
class main_class
{
private $test2;
public function __construct()
{
$this->test2 = array();
include("./test1.php");
$var_name = "test";
$this->$var_name = new test1();
}
protected function do_include()
{
include("./test2.php");
$this->test2["test2"] = new test2();
}
}
$test = new main_class();
?>
test1.php:
class test1 extends main_class
{
public function __construct()
{
$this->do_include();
}
}
?>
test2.php:
class test2 extends test1
{
public function __construct()
{
print_r($this->test);
}
}
?>
このコードでは、次のエラーが発生します: Notice: Undefined property: test2::$test
前もって感謝します...