Table des matières
在依赖注入里如何进行多组件间的调用
Jun 06, 2016 pm 08:27 PM
oop
php
<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>
Copier après la connexion
Copier après la connexion
这里的老师是获取不到学生名字的
我用容器将所有的应用组件包起来,然后,当应用组件之间调用的时候,就会有这样的问题。
前面注册进去的应用组件无法调用后面注册的应用,但是后面注册的应用是可以调用前面的。
那么如果说,前面的要使用后面的应用,这个问题怎么解决呢?
回复内容:
<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>
Copier après la connexion
Copier après la connexion
这里的老师是获取不到学生名字的
我用容器将所有的应用组件包起来,然后,当应用组件之间调用的时候,就会有这样的问题。
前面注册进去的应用组件无法调用后面注册的应用,但是后面注册的应用是可以调用前面的。
那么如果说,前面的要使用后面的应用,这个问题怎么解决呢?
<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>
Copier après la connexion
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines
By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Difficulté à mettre à jour la mise en cache des pages Web officielles du compte: comment éviter l'ancien cache affectant l'expérience utilisateur après la mise à jour de la version?
3 Il y a quelques semaines
By 王林
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌

Outils chauds Tags

Article chaud
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines
By DDD
Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌
Difficulté à mettre à jour la mise en cache des pages Web officielles du compte: comment éviter l'ancien cache affectant l'expérience utilisateur après la mise à jour de la version?
3 Il y a quelques semaines
By 王林
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines
By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Comment configurer Visual Studio Code (VS Code) pour le développement PHP
