How to call multiple components in dependency Injection

Source: Internet
Author: User
{Code ...} here, the teacher cannot get the Student name. I use containers to pack all application components. Then, this problem occurs when application components are called between them. The application component registered earlier cannot call the application registered later, but the application registered later can call...

  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 cannot obtain the Student name.

I use containers to package all application components. Then, this problem occurs when application components are called between them.

The application component registered earlier cannot call the application registered later, but the application registered later can call the previous one.

So how can we solve this problem if we want to use the following applications?

Reply content:

  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 cannot obtain the Student name.

I use containers to package all application components. Then, this problem occurs when application components are called between them.

The application component registered earlier cannot call the application registered later, but the application registered later can call the previous one.

So how can we solve this problem if we want to use the following applications?

  
  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.