room['teacher'] = $teacher; } function addStudents($student){ $this->room['student'] = $student; }}class Teacher{ public $name='AAAA'; public $room=[]; function __construct($room){ $this->room = $room; } function sayHello(){ echo ' hello '.$this->room['student']->name.' student '.PHP_EOL; }}class Student{ public $name = 'BBB'; public $room=[]; function __construct($room){ $this->room = $room; } function sayHello(){ echo ' hello '.$this->room['teacher']->name. ' teacher '.PHP_EOL; }}$Classroom = new Classroom;$Classroom->addTeacher( new Teacher($Classroom->room) );$Classroom->addStudents( new Student($Classroom->room) );$Classroom->room['teacher']->sayHello();$Classroom->room['student']->sayHello();
這裡的老師是擷取不到學生名字的
我用容器將所有的應用組件包起來,然後,當應用組件之間調用的時候,就會有這樣的問題。
前面註冊進去的應用組件無法調用後面註冊的應用,但是後面註冊的應用是可以調用前面的。
那麼如果說,前面的要使用後面的應用,這個問題怎麼解決呢?
回複內容:
room['teacher'] = $teacher; } function addStudents($student){ $this->room['student'] = $student; }}class Teacher{ public $name='AAAA'; public $room=[]; function __construct($room){ $this->room = $room; } function sayHello(){ echo ' hello '.$this->room['student']->name.' student '.PHP_EOL; }}class Student{ public $name = 'BBB'; public $room=[]; function __construct($room){ $this->room = $room; } function sayHello(){ echo ' hello '.$this->room['teacher']->name. ' teacher '.PHP_EOL; }}$Classroom = new Classroom;$Classroom->addTeacher( new Teacher($Classroom->room) );$Classroom->addStudents( new Student($Classroom->room) );$Classroom->room['teacher']->sayHello();$Classroom->room['student']->sayHello();
這裡的老師是擷取不到學生名字的
我用容器將所有的應用組件包起來,然後,當應用組件之間調用的時候,就會有這樣的問題。
前面註冊進去的應用組件無法調用後面註冊的應用,但是後面註冊的應用是可以調用前面的。
那麼如果說,前面的要使用後面的應用,這個問題怎麼解決呢?
room['teacher'] = $teacher; } function addStudents($student){ $this->room['student'] = $student; } } class Teacher{ public $name='AAAA'; public $room=[]; function __construct($room){ $this->room = $room; } function sayHello(){ echo ' hello '.$this->room->room['student']->name.' sdent '.PHP_EOL; } } class Student{ public $name = 'BBB'; public $room=[]; function __construct($room){ $this->room = $room; } function sayHello(){ echo ' hello '.$this->room->room['teacher']->name. ' teacher '.PHP_EOL; } } $Classroom = new Classroom; $Classroom->addTeacher( new Teacher($Classroom) ); $Classroom->addStudents( new Student($Classroom) ); $Classroom->room['teacher']->sayHello(); $Classroom->room['student']->sayHello();