How to make calls between multiple components in a dependency injection

Source: Internet
Author: User
 !--? phpclass classroom{public $room = [];    function Addteacher ($teacher) {$this--->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 ();  

The teacher here is not getting the student's name.

I wrap all the application components in a container, and then, when called between application components, there is a problem.

The application component that was previously registered cannot invoke the app that is registered later, but the application that is registered later can call the previous one.

So if the previous application is to be used, how can the problem be solved?

Reply content:

 !--? phpclass classroom{public $room = [];    function Addteacher ($teacher) {$this--->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 ();  

The teacher here is not getting the student's name.

I wrap all the application components in a container, and then, when called between application components, there is a problem.

The application component that was previously registered cannot invoke the app that is registered later, but the application that is registered later can call the previous one.

So if the previous application is to be used, how can the problem be solved?

  
  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 ();
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.