Heim  >  Artikel  >  Backend-Entwicklung  >  在依赖注入里如何进行多组件间的调用

在依赖注入里如何进行多组件间的调用

WBOY
WBOYOriginal
2016-06-06 20:27:561265Durchsuche

<code><?php class 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();
</code>

这里的老师是获取不到学生名字的

我用容器将所有的应用组件包起来,然后,当应用组件之间调用的时候,就会有这样的问题。

前面注册进去的应用组件无法调用后面注册的应用,但是后面注册的应用是可以调用前面的。

那么如果说,前面的要使用后面的应用,这个问题怎么解决呢?

回复内容:

<code><?php class 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();
</code>

这里的老师是获取不到学生名字的

我用容器将所有的应用组件包起来,然后,当应用组件之间调用的时候,就会有这样的问题。

前面注册进去的应用组件无法调用后面注册的应用,但是后面注册的应用是可以调用前面的。

那么如果说,前面的要使用后面的应用,这个问题怎么解决呢?

<code>  <?php class 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->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();
</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn