在依賴注入裡如何進行多組件間的調用

來源:互聯網
上載者:User
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();
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.