├── LICENSE ├── README.md ├── step01.php ├── step02.php ├── step03.php ├── step04.php ├── step05.php ├── step06.php ├── step07.php ├── step08.php ├── step09.php ├── step10.php └── step11.php /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php_di 2 | 简单的PHP依赖注入示例 3 | 欢迎大家一起探讨交流 4 | 5 | -------------------------------------------------------------------------------- /step01.php: -------------------------------------------------------------------------------- 1 | doSomething(); 17 | echo __METHOD__, '|'; 18 | } 19 | } 20 | 21 | class Foo 22 | { 23 | public function doSomething() 24 | { 25 | $bar = new Bar(); 26 | $bar->doSomething(); 27 | echo __METHOD__; 28 | } 29 | } 30 | 31 | $foo = new Foo(); 32 | $foo->doSomething(); //Bim::doSomething|Bar::doSomething|Foo::doSomething -------------------------------------------------------------------------------- /step02.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 18 | } 19 | 20 | public function doSomething() 21 | { 22 | $this->bim->doSomething(); 23 | echo __METHOD__, '|'; 24 | } 25 | } 26 | 27 | class Foo 28 | { 29 | private $bar; 30 | 31 | public function __construct(Bar $bar) 32 | { 33 | $this->bar = $bar; 34 | } 35 | 36 | public function doSomething() 37 | { 38 | $this->bar->doSomething(); 39 | echo __METHOD__; 40 | } 41 | } 42 | 43 | $foo = new Foo( new Bar( new Bim() ) ); 44 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething -------------------------------------------------------------------------------- /step03.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 17 | } 18 | 19 | public function doSomething() 20 | { 21 | $this->bim->doSomething(); 22 | echo __METHOD__, '|'; 23 | } 24 | } 25 | 26 | class Foo 27 | { 28 | private $bar; 29 | 30 | public function __construct(Bar $bar) 31 | { 32 | $this->bar = $bar; 33 | } 34 | 35 | public function doSomething() 36 | { 37 | $this->bar->doSomething(); 38 | echo __METHOD__; 39 | } 40 | } 41 | 42 | class Container 43 | { 44 | private $s = array(); 45 | 46 | function __set($k, $c) 47 | { 48 | $this->s[$k] = $c; 49 | } 50 | 51 | function __get($k) 52 | { 53 | return $this->s[$k]($this); 54 | } 55 | } 56 | 57 | $c = new Container(); 58 | 59 | $c->bim = function () { 60 | return new Bim(); 61 | }; 62 | 63 | $c->bar = function ($c) { 64 | return new Bar($c->bim); 65 | }; 66 | 67 | $c->foo = function ($c) { 68 | return new Foo($c->bar); 69 | }; 70 | 71 | // 从容器中取得Foo 72 | $foo = $c->foo; 73 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 74 | echo "\n"; 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /step04.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 17 | } 18 | 19 | public function doSomething() 20 | { 21 | $this->bim->doSomething(); 22 | echo __METHOD__, '|'; 23 | } 24 | } 25 | 26 | class Foo 27 | { 28 | private $bar; 29 | 30 | public function __construct(Bar $bar) 31 | { 32 | $this->bar = $bar; 33 | } 34 | 35 | public function doSomething() 36 | { 37 | $this->bar->doSomething(); 38 | echo __METHOD__; 39 | } 40 | } 41 | 42 | class IoC 43 | { 44 | protected static $registry = []; 45 | 46 | public static function bind($name, Callable $resolver) 47 | { 48 | static::$registry[$name] = $resolver; 49 | } 50 | 51 | public static function make($name) 52 | { 53 | if (isset(static::$registry[$name])) { 54 | $resolver = static::$registry[$name]; 55 | return $resolver(); 56 | } 57 | throw new Exception('Alias does not exist in the IoC registry.'); 58 | } 59 | } 60 | 61 | IoC::bind('bim', function () { 62 | return new Bim(); 63 | }); 64 | 65 | IoC::bind('bar', function () { 66 | return new Bar(IoC::make('bim')); 67 | }); 68 | 69 | IoC::bind('foo', function () { 70 | return new Foo(IoC::make('bar')); 71 | }); 72 | 73 | 74 | // 从容器中取得Foo 75 | $foo = IoC::make('foo'); 76 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 77 | echo "\n"; 78 | -------------------------------------------------------------------------------- /step05.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 17 | } 18 | 19 | public function doSomething() 20 | { 21 | $this->bim->doSomething(); 22 | echo __METHOD__, '|'; 23 | } 24 | } 25 | 26 | class Foo 27 | { 28 | private $bar; 29 | 30 | public function __construct(Bar $bar) 31 | { 32 | $this->bar = $bar; 33 | } 34 | 35 | public function doSomething() 36 | { 37 | $this->bar->doSomething(); 38 | echo __METHOD__; 39 | } 40 | } 41 | 42 | class Container 43 | { 44 | private $s = array(); 45 | 46 | public function __set($k, $c) 47 | { 48 | $this->s[$k] = $c; 49 | } 50 | 51 | public function __get($k) 52 | { 53 | // return $this->s[$k]($this); 54 | return $this->build($this->s[$k]); 55 | } 56 | 57 | /** 58 | * 自动绑定(Autowiring)自动解析(Automatic Resolution) 59 | * 60 | * @param string $className 61 | * @return object 62 | * @throws Exception 63 | */ 64 | public function build($className) 65 | { 66 | // 如果是匿名函数(Anonymous functions),也叫闭包函数(closures) 67 | if ($className instanceof Closure) { 68 | // 执行闭包函数,并将结果 69 | return $className($this); 70 | } 71 | 72 | /** @var ReflectionClass $reflector */ 73 | $reflector = new ReflectionClass($className); 74 | 75 | // 检查类是否可实例化, 排除抽象类abstract和对象接口interface 76 | if (!$reflector->isInstantiable()) { 77 | throw new Exception("Can't instantiate this."); 78 | } 79 | 80 | /** @var ReflectionMethod $constructor 获取类的构造函数 */ 81 | $constructor = $reflector->getConstructor(); 82 | 83 | // 若无构造函数,直接实例化并返回 84 | if (is_null($constructor)) { 85 | return new $className; 86 | } 87 | 88 | // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 89 | $parameters = $constructor->getParameters(); 90 | 91 | // 递归解析构造函数的参数 92 | $dependencies = $this->getDependencies($parameters); 93 | 94 | // 创建一个类的新实例,给出的参数将传递到类的构造函数。 95 | return $reflector->newInstanceArgs($dependencies); 96 | } 97 | 98 | /** 99 | * @param array $parameters 100 | * @return array 101 | * @throws Exception 102 | */ 103 | public function getDependencies($parameters) 104 | { 105 | $dependencies = []; 106 | 107 | /** @var ReflectionParameter $parameter */ 108 | foreach ($parameters as $parameter) { 109 | /** @var ReflectionClass $dependency */ 110 | $dependency = $parameter->getClass(); 111 | 112 | if (is_null($dependency)) { 113 | // 是变量,有默认值则设置默认值 114 | $dependencies[] = $this->resolveNonClass($parameter); 115 | } else { 116 | // 是一个类,递归解析 117 | $dependencies[] = $this->build($dependency->name); 118 | } 119 | } 120 | 121 | return $dependencies; 122 | } 123 | 124 | /** 125 | * @param ReflectionParameter $parameter 126 | * @return mixed 127 | * @throws Exception 128 | */ 129 | public function resolveNonClass($parameter) 130 | { 131 | // 有默认值则返回默认值 132 | if ($parameter->isDefaultValueAvailable()) { 133 | return $parameter->getDefaultValue(); 134 | } 135 | 136 | throw new Exception('I have no idea what to do here.'); 137 | } 138 | } 139 | 140 | // ---- 141 | $c = new Container(); 142 | $c->bar = 'Bar'; 143 | $c->foo = function ($c) { 144 | return new Foo($c->bar); 145 | }; 146 | // 从容器中取得Foo 147 | $foo = $c->foo; 148 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 149 | 150 | // ---- 151 | $di = new Container(); 152 | 153 | $di->foo = 'Foo'; 154 | 155 | /** @var Foo $foo */ 156 | $foo = $di->foo; 157 | 158 | var_dump($foo); 159 | /* 160 | Foo#10 (1) { 161 | private $bar => 162 | class Bar#14 (1) { 163 | private $bim => 164 | class Bim#16 (0) { 165 | } 166 | } 167 | } 168 | */ 169 | 170 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 171 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /step06.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 17 | } 18 | 19 | public function doSomething() 20 | { 21 | $this->bim->doSomething(); 22 | echo __METHOD__, '|'; 23 | } 24 | } 25 | 26 | class Foo 27 | { 28 | private $bar; 29 | 30 | public function __construct(Bar $bar) 31 | { 32 | $this->bar = $bar; 33 | } 34 | 35 | public function doSomething() 36 | { 37 | $this->bar->doSomething(); 38 | echo __METHOD__; 39 | } 40 | } 41 | 42 | class Container 43 | { 44 | private $s = array(); 45 | 46 | public function __set($k, $c) 47 | { 48 | $this->s[$k] = $c; 49 | } 50 | 51 | public function __get($k) 52 | { 53 | // return $this->s[$k]($this); 54 | $result = $this->build($this->s[$k]); 55 | return $result; 56 | } 57 | 58 | public function getS () { 59 | return $this->s; 60 | } 61 | 62 | /** 63 | * 自动绑定(Autowiring)自动解析(Automatic Resolution) 64 | * 65 | * @param string $className 66 | * @return object 67 | * @throws Exception 68 | */ 69 | public function build($className) 70 | { 71 | // 如果是匿名函数(Anonymous functions),也叫闭包函数(closures) 72 | //var_dump($className instanceof Closure);// bool(true) 73 | if ($className instanceof Closure) { 74 | // 执行闭包函数,并将结果 75 | //var_dump($className); 76 | //print_r($this); 77 | /* 78 | * object(Closure)#2 (1) { 79 | ["parameter"]=> 80 | array(1) { 81 | ["$c"]=> 82 | string(10) "" 83 | } 84 | } 85 | 86 | function ($c) { 87 | $bar = $c->bar; 88 | return new Foo($bar); 89 | }; 90 | 91 | 92 | 93 | 94 | 95 | Container Object 96 | ( 97 | [s:Container:private] => Array 98 | ( 99 | [bar] => Bar 100 | [foo] => Closure Object 101 | ( 102 | [parameter] => Array 103 | ( 104 | [$c] => 105 | ) 106 | 107 | ) 108 | 109 | ) 110 | 111 | ) 112 | 113 | * 114 | */ 115 | $o = $className($this); 116 | return $o; 117 | } 118 | 119 | /** @var ReflectionClass $reflector */ 120 | $reflector = new ReflectionClass($className); 121 | 122 | // 检查类是否可实例化, 排除抽象类abstract和对象接口interface 123 | if (!$reflector->isInstantiable()) { 124 | throw new Exception("Can't instantiate this."); 125 | } 126 | 127 | /** @var ReflectionMethod $constructor 获取类的构造函数 */ 128 | $constructor = $reflector->getConstructor(); 129 | 130 | // 若无构造函数,直接实例化并返回 131 | if (is_null($constructor)) { 132 | return new $className; 133 | } 134 | 135 | // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 136 | $parameters = $constructor->getParameters(); 137 | 138 | // 递归解析构造函数的参数 139 | $dependencies = $this->getDependencies($parameters); 140 | 141 | // 创建一个类的新实例,给出的参数将传递到类的构造函数。 142 | return $reflector->newInstanceArgs($dependencies); 143 | } 144 | 145 | /** 146 | * @param array $parameters 147 | * @return array 148 | * @throws Exception 149 | */ 150 | public function getDependencies($parameters) 151 | { 152 | $dependencies = []; 153 | 154 | /** @var ReflectionParameter $parameter */ 155 | foreach ($parameters as $parameter) { 156 | /** @var ReflectionClass $dependency */ 157 | $dependency = $parameter->getClass(); 158 | 159 | if (is_null($dependency)) { 160 | // 是变量,有默认值则设置默认值 161 | $dependencies[] = $this->resolveNonClass($parameter); 162 | } else { 163 | // 是一个类,递归解析 164 | $dependencies[] = $this->build($dependency->name); 165 | } 166 | } 167 | 168 | return $dependencies; 169 | } 170 | 171 | /** 172 | * @param ReflectionParameter $parameter 173 | * @return mixed 174 | * @throws Exception 175 | */ 176 | public function resolveNonClass($parameter) 177 | { 178 | // 有默认值则返回默认值 179 | if ($parameter->isDefaultValueAvailable()) { 180 | return $parameter->getDefaultValue(); 181 | } 182 | 183 | throw new Exception('I have no idea what to do here.'); 184 | } 185 | } 186 | 187 | // ---- 188 | $c = new Container(); 189 | //print_r($c); 190 | /* 191 | * Container Object 192 | ( 193 | [s:Container:private] => Array 194 | ( 195 | ) 196 | 197 | ) 198 | */ 199 | $c->bar = 'Bar'; 200 | $c->foo = function ($c) { 201 | print_r($c); 202 | /* 203 | * Container Object 204 | ( 205 | [s:Container:private] => Array 206 | ( 207 | [bar] => Bar 208 | [foo] => Closure Object 209 | ( 210 | [parameter] => Array 211 | ( 212 | [$c] => 213 | ) 214 | 215 | ) 216 | 217 | ) 218 | 219 | ) 220 | * 221 | */ 222 | $bar = $c->bar; 223 | return new Foo($bar); 224 | }; 225 | //print_r($c->getS()); 226 | /* 227 | * Array 228 | ( 229 | [bar] => Bar 230 | [foo] => Closure Object 231 | ( 232 | [parameter] => Array 233 | ( 234 | [$c] => 235 | ) 236 | 237 | ) 238 | 239 | ) 240 | 241 | * 242 | */ 243 | 244 | // 从容器中取得Foo 245 | $foo = $c->foo; 246 | print_r($foo); 247 | /* 248 | * Foo Object 249 | ( 250 | [bar:Foo:private] => Bar Object 251 | ( 252 | [bim:Bar:private] => Bim Object 253 | ( 254 | ) 255 | 256 | ) 257 | 258 | ) 259 | 260 | * 261 | */ 262 | exit; 263 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 264 | 265 | // ---- 266 | $di = new Container(); 267 | 268 | $di->foo = 'Foo'; 269 | 270 | /** @var Foo $foo */ 271 | $foo = $di->foo; 272 | 273 | var_dump($foo); 274 | /* 275 | Foo#10 (1) { 276 | private $bar => 277 | class Bar#14 (1) { 278 | private $bim => 279 | class Bim#16 (0) { 280 | } 281 | } 282 | } 283 | */ 284 | 285 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 286 | 287 | 288 | 289 | -------------------------------------------------------------------------------- /step07.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 17 | } 18 | 19 | public function doSomething() 20 | { 21 | $this->bim->doSomething(); 22 | echo __METHOD__, '|'; 23 | } 24 | } 25 | 26 | class Foo 27 | { 28 | private $bar; 29 | 30 | public function __construct(Bar $bar) 31 | { 32 | $this->bar = $bar; 33 | } 34 | 35 | public function doSomething() 36 | { 37 | $this->bar->doSomething(); 38 | echo __METHOD__; 39 | } 40 | } 41 | 42 | class Container 43 | { 44 | private $s = array(); 45 | 46 | public function __set($k, $c) 47 | { 48 | $this->s[$k] = $c; 49 | } 50 | 51 | public function __get($k) 52 | { 53 | // return $this->s[$k]($this); 54 | $result = $this->build($this->s[$k]); 55 | return $result; 56 | } 57 | 58 | public function getS () { 59 | return $this->s; 60 | } 61 | 62 | /** 63 | * 自动绑定(Autowiring)自动解析(Automatic Resolution) 64 | * 65 | * @param string $className 66 | * @return object 67 | * @throws Exception 68 | */ 69 | public function build($className) 70 | { 71 | // 如果是匿名函数(Anonymous functions),也叫闭包函数(closures) 72 | //var_dump($className instanceof Closure);// bool(false) 73 | if ($className instanceof Closure) { 74 | // 执行闭包函数,并将结果 75 | $o = $className($this); 76 | return $o; 77 | } 78 | 79 | /** @var ReflectionClass $reflector */ 80 | //var_dump($className);// string(3) "Bim" 81 | $reflector = new ReflectionClass($className); 82 | //print_r($reflector); 83 | /* 84 | * ReflectionClass Object 85 | ( 86 | [name] => Bim 87 | ) 88 | 89 | * 90 | */ 91 | 92 | // 检查类是否可实例化, 排除抽象类abstract和对象接口interface 93 | //var_dump($reflector->isInstantiable());// bool(true) 94 | if (!$reflector->isInstantiable()) { 95 | throw new Exception("Can't instantiate this."); 96 | } 97 | 98 | /** @var ReflectionMethod $constructor 获取类的构造函数 */ 99 | $constructor = $reflector->getConstructor(); 100 | //var_dump($constructor);// NULL 101 | 102 | // 若无构造函数,直接实例化并返回 103 | if (is_null($constructor)) { 104 | $b = new $className; 105 | //print_r($b); 106 | /* 107 | * Bim Object 108 | ( 109 | ) 110 | 111 | * 112 | */ 113 | return $b; 114 | } 115 | 116 | // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 117 | $parameters = $constructor->getParameters(); 118 | 119 | // 递归解析构造函数的参数 120 | $dependencies = $this->getDependencies($parameters); 121 | 122 | // 创建一个类的新实例,给出的参数将传递到类的构造函数。 123 | return $reflector->newInstanceArgs($dependencies); 124 | } 125 | 126 | /** 127 | * @param array $parameters 128 | * @return array 129 | * @throws Exception 130 | */ 131 | public function getDependencies($parameters) 132 | { 133 | $dependencies = []; 134 | 135 | /** @var ReflectionParameter $parameter */ 136 | foreach ($parameters as $parameter) { 137 | /** @var ReflectionClass $dependency */ 138 | $dependency = $parameter->getClass(); 139 | 140 | if (is_null($dependency)) { 141 | // 是变量,有默认值则设置默认值 142 | $dependencies[] = $this->resolveNonClass($parameter); 143 | } else { 144 | // 是一个类,递归解析 145 | $dependencies[] = $this->build($dependency->name); 146 | } 147 | } 148 | 149 | return $dependencies; 150 | } 151 | 152 | /** 153 | * @param ReflectionParameter $parameter 154 | * @return mixed 155 | * @throws Exception 156 | */ 157 | public function resolveNonClass($parameter) 158 | { 159 | // 有默认值则返回默认值 160 | if ($parameter->isDefaultValueAvailable()) { 161 | return $parameter->getDefaultValue(); 162 | } 163 | 164 | throw new Exception('I have no idea what to do here.'); 165 | } 166 | } 167 | 168 | // ---- 169 | $c = new Container(); 170 | //print_r($c); 171 | /* 172 | * Container Object 173 | ( 174 | [s:Container:private] => Array 175 | ( 176 | ) 177 | 178 | ) 179 | */ 180 | 181 | $c->bim = 'Bim'; 182 | //print_r($c->getS()); 183 | /* 184 | * Array 185 | ( 186 | [bim] => Bim 187 | ) 188 | 189 | */ 190 | $bim = $c->bim; 191 | $bim->doSomething(); // Bim::doSomething| 192 | 193 | -------------------------------------------------------------------------------- /step08.php: -------------------------------------------------------------------------------- 1 | i = $i; 9 | $this->j = $j; 10 | } 11 | 12 | public function doSomething() 13 | { 14 | echo __METHOD__, '|'; 15 | } 16 | } 17 | 18 | class Bar 19 | { 20 | private $bim; 21 | 22 | public function __construct(Bim $bim) 23 | { 24 | $this->bim = $bim; 25 | } 26 | 27 | public function doSomething() 28 | { 29 | $this->bim->doSomething(); 30 | echo __METHOD__, '|'; 31 | } 32 | } 33 | 34 | class Foo 35 | { 36 | private $bar; 37 | 38 | public function __construct(Bar $bar) 39 | { 40 | $this->bar = $bar; 41 | } 42 | 43 | public function doSomething() 44 | { 45 | $this->bar->doSomething(); 46 | echo __METHOD__; 47 | } 48 | } 49 | 50 | class Container 51 | { 52 | private $s = array(); 53 | 54 | public function __set($k, $c) 55 | { 56 | $this->s[$k] = $c; 57 | } 58 | 59 | public function __get($k) 60 | { 61 | // return $this->s[$k]($this); 62 | $result = $this->build($this->s[$k]); 63 | return $result; 64 | } 65 | 66 | public function getS () { 67 | return $this->s; 68 | } 69 | 70 | /** 71 | * 自动绑定(Autowiring)自动解析(Automatic Resolution) 72 | * 73 | * @param string $className 74 | * @return object 75 | * @throws Exception 76 | */ 77 | public function build($className) 78 | { 79 | // 如果是匿名函数(Anonymous functions),也叫闭包函数(closures) 80 | //var_dump($className instanceof Closure);// bool(false) 81 | if ($className instanceof Closure) { 82 | // 执行闭包函数,并将结果 83 | $o = $className($this); 84 | return $o; 85 | } 86 | 87 | /** @var ReflectionClass $reflector */ 88 | //var_dump($className);// string(3) "Bim" 89 | $reflector = new ReflectionClass($className); 90 | //print_r($reflector); 91 | /* 92 | * ReflectionClass Object 93 | ( 94 | [name] => Bim 95 | ) 96 | 97 | * 98 | */ 99 | 100 | // 检查类是否可实例化, 排除抽象类abstract和对象接口interface 101 | //var_dump($reflector->isInstantiable());// bool(true) 102 | if (!$reflector->isInstantiable()) { 103 | throw new Exception("Can't instantiate this."); 104 | } 105 | 106 | /** @var ReflectionMethod $constructor 获取类的构造函数 */ 107 | $constructor = $reflector->getConstructor(); 108 | //var_dump($constructor); 109 | /* 110 | * object(ReflectionMethod)#3 (2) { 111 | ["name"]=> 112 | string(11) "__construct" 113 | ["class"]=> 114 | string(3) "Bim" 115 | } 116 | 117 | * 118 | */ 119 | 120 | // 若无构造函数,直接实例化并返回 121 | if (is_null($constructor)) { 122 | $b = new $className; 123 | //print_r($b); 124 | /* 125 | * Bim Object 126 | ( 127 | ) 128 | 129 | * 130 | */ 131 | return $b; 132 | } 133 | 134 | // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 135 | $parameters = $constructor->getParameters(); 136 | //print_r($parameters); 137 | /* 138 | * Array 139 | ( 140 | [0] => ReflectionParameter Object 141 | ( 142 | [name] => i 143 | ) 144 | 145 | [1] => ReflectionParameter Object 146 | ( 147 | [name] => j 148 | ) 149 | 150 | ) 151 | 152 | * 153 | */ 154 | 155 | // 递归解析构造函数的参数 156 | $dependencies = $this->getDependencies($parameters); 157 | 158 | // 创建一个类的新实例,给出的参数将传递到类的构造函数。 159 | return $reflector->newInstanceArgs($dependencies); 160 | } 161 | 162 | /** 163 | * @param array $parameters 164 | * @return array 165 | * @throws Exception 166 | */ 167 | /* 168 | * Array 169 | ( 170 | [0] => ReflectionParameter Object 171 | ( 172 | [name] => i 173 | ) 174 | 175 | [1] => ReflectionParameter Object 176 | ( 177 | [name] => j 178 | ) 179 | 180 | ) 181 | 182 | * 183 | */ 184 | public function getDependencies($parameters) 185 | { 186 | $dependencies = []; 187 | 188 | /** @var ReflectionParameter $parameter */ 189 | foreach ($parameters as $parameter) { 190 | /** @var ReflectionClass $dependency */ 191 | $dependency = $parameter->getClass(); 192 | var_dump($dependency);// NULL 193 | 194 | if (is_null($dependency)) { 195 | // 是变量,有默认值则设置默认值 196 | $dependencies[] = $this->resolveNonClass($parameter); 197 | } else { 198 | // 是一个类,递归解析 199 | $dependencies[] = $this->build($dependency->name); 200 | } 201 | } 202 | print_r($dependencies); 203 | 204 | return $dependencies; 205 | } 206 | 207 | /** 208 | * @param ReflectionParameter $parameter 209 | * @return mixed 210 | * @throws Exception 211 | */ 212 | public function resolveNonClass($parameter) 213 | { 214 | // 有默认值则返回默认值 215 | var_dump($parameter->isDefaultValueAvailable());//bool(false) 216 | if ($parameter->isDefaultValueAvailable()) { 217 | return $parameter->getDefaultValue(); 218 | } 219 | 220 | //throw new Exception('I have no idea what to do here.'); 221 | } 222 | } 223 | 224 | // ---- 225 | $c = new Container(); 226 | //print_r($c); 227 | /* 228 | * Container Object 229 | ( 230 | [s:Container:private] => Array 231 | ( 232 | ) 233 | 234 | ) 235 | */ 236 | 237 | $c->bim = 'Bim'; 238 | //print_r($c->getS()); 239 | /* 240 | * Array 241 | ( 242 | [bim] => Bim 243 | ) 244 | 245 | */ 246 | $bim = $c->bim; 247 | $bim->doSomething(); // Bim::doSomething| 248 | 249 | -------------------------------------------------------------------------------- /step09.php: -------------------------------------------------------------------------------- 1 | i = $i; 9 | $this->j = $j; 10 | } 11 | 12 | public function doSomething() 13 | { 14 | echo __METHOD__, '|'; 15 | } 16 | } 17 | 18 | class Bar 19 | { 20 | private $bim; 21 | 22 | public function __construct(Bim $bim) 23 | { 24 | $this->bim = $bim; 25 | } 26 | 27 | public function doSomething() 28 | { 29 | $this->bim->doSomething(); 30 | echo __METHOD__, '|'; 31 | } 32 | } 33 | 34 | class Foo 35 | { 36 | private $bar; 37 | 38 | public function __construct(Bar $bar) 39 | { 40 | $this->bar = $bar; 41 | } 42 | 43 | public function doSomething() 44 | { 45 | $this->bar->doSomething(); 46 | echo __METHOD__; 47 | } 48 | } 49 | 50 | class Container 51 | { 52 | private $s = array(); 53 | 54 | public function __set($k, $c) 55 | { 56 | $this->s[$k] = $c; 57 | } 58 | 59 | public function __get($k) 60 | { 61 | // return $this->s[$k]($this); 62 | $result = $this->build($this->s[$k]); 63 | return $result; 64 | } 65 | 66 | public function getS () { 67 | return $this->s; 68 | } 69 | 70 | /** 71 | * 自动绑定(Autowiring)自动解析(Automatic Resolution) 72 | * 73 | * @param string $className 74 | * @return object 75 | * @throws Exception 76 | */ 77 | public function build($className) 78 | { 79 | //print_r($className); 80 | /* 81 | * Array 82 | ( 83 | [Bim] => Array 84 | ( 85 | [0] => 1 86 | [1] => 2 87 | ) 88 | 89 | ) 90 | 91 | * 92 | */ 93 | // 如果是匿名函数(Anonymous functions),也叫闭包函数(closures) 94 | //var_dump($className instanceof Closure);// bool(false) 95 | if ($className instanceof Closure) { 96 | // 执行闭包函数,并将结果 97 | $o = $className($this); 98 | return $o; 99 | } 100 | 101 | /** @var ReflectionClass $reflector */ 102 | //var_dump($className);// string(3) "Bim" 103 | if (!is_array($className)) { 104 | throw new Exception('here must array'); 105 | } 106 | 107 | foreach ($className as $key => $value) { 108 | $class = $key; 109 | foreach ($value as $v) { 110 | $param[] = $v; 111 | } 112 | 113 | } 114 | //var_dump($class); 115 | //print_r($param); 116 | /* 117 | * string(3) "Bim" 118 | Array 119 | ( 120 | [0] => 1 121 | [1] => 2 122 | ) 123 | 124 | * 125 | */ 126 | $reflector = new ReflectionClass($class); 127 | //print_r($reflector); 128 | /* 129 | * ReflectionClass Object 130 | ( 131 | [name] => Bim 132 | ) 133 | 134 | * 135 | */ 136 | // 检查类是否可实例化, 排除抽象类abstract和对象接口interface 137 | //var_dump($reflector->isInstantiable());// bool(true) 138 | if (!$reflector->isInstantiable()) { 139 | throw new Exception("Can't instantiate this."); 140 | } 141 | 142 | /** @var ReflectionMethod $constructor 获取类的构造函数 */ 143 | $constructor = $reflector->getConstructor(); 144 | //var_dump($constructor); 145 | /* 146 | * object(ReflectionMethod)#3 (2) { 147 | ["name"]=> 148 | string(11) "__construct" 149 | ["class"]=> 150 | string(3) "Bim" 151 | } 152 | 153 | * 154 | */ 155 | 156 | // 若无构造函数,直接实例化并返回 157 | if (is_null($constructor)) { 158 | $b = new $className; 159 | //print_r($b); 160 | /* 161 | * Bim Object 162 | ( 163 | ) 164 | 165 | * 166 | */ 167 | return $b; 168 | } 169 | 170 | // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 171 | //$parameters = $constructor->getParameters(); 172 | //print_r($parameters); 173 | /* 174 | * Array 175 | ( 176 | [0] => ReflectionParameter Object 177 | ( 178 | [name] => i 179 | ) 180 | 181 | [1] => ReflectionParameter Object 182 | ( 183 | [name] => j 184 | ) 185 | 186 | ) 187 | 188 | * 189 | */ 190 | 191 | // 递归解析构造函数的参数 192 | //$dependencies = $this->getDependencies($parameters); 193 | 194 | // 创建一个类的新实例,给出的参数将传递到类的构造函数。 195 | return $reflector->newInstanceArgs($param); 196 | } 197 | 198 | /** 199 | * @param array $parameters 200 | * @return array 201 | * @throws Exception 202 | */ 203 | /* 204 | * Array 205 | ( 206 | [0] => ReflectionParameter Object 207 | ( 208 | [name] => i 209 | ) 210 | 211 | [1] => ReflectionParameter Object 212 | ( 213 | [name] => j 214 | ) 215 | 216 | ) 217 | 218 | * 219 | */ 220 | public function getDependencies($parameters) 221 | { 222 | $dependencies = []; 223 | 224 | /** @var ReflectionParameter $parameter */ 225 | foreach ($parameters as $parameter) { 226 | /** @var ReflectionClass $dependency */ 227 | $dependency = $parameter->getClass(); 228 | var_dump($dependency);// NULL 229 | 230 | if (is_null($dependency)) { 231 | // 是变量,有默认值则设置默认值 232 | $dependencies[] = $this->resolveNonClass($parameter); 233 | } else { 234 | // 是一个类,递归解析 235 | $dependencies[] = $this->build($dependency->name); 236 | } 237 | } 238 | print_r($dependencies); 239 | 240 | return $dependencies; 241 | } 242 | 243 | /** 244 | * @param ReflectionParameter $parameter 245 | * @return mixed 246 | * @throws Exception 247 | */ 248 | public function resolveNonClass($parameter) 249 | { 250 | // 有默认值则返回默认值 251 | var_dump($parameter->isDefaultValueAvailable());//bool(false) 252 | if ($parameter->isDefaultValueAvailable()) { 253 | return $parameter->getDefaultValue(); 254 | } 255 | 256 | //throw new Exception('I have no idea what to do here.'); 257 | } 258 | } 259 | 260 | // ---- 261 | $c = new Container(); 262 | //print_r($c); 263 | /* 264 | * Container Object 265 | ( 266 | [s:Container:private] => Array 267 | ( 268 | ) 269 | 270 | ) 271 | */ 272 | 273 | $c->bim = [ 'Bim' => [1, 2] ]; 274 | //print_r($c->getS()); 275 | /* 276 | * Array 277 | ( 278 | [bim] => Array 279 | ( 280 | [Bim] => Array 281 | ( 282 | [0] => 1 283 | [1] => 2 284 | ) 285 | 286 | ) 287 | 288 | ) 289 | 290 | */ 291 | 292 | $bim = $c->bim; 293 | $bim->doSomething(); // Bim::doSomething| 294 | 295 | -------------------------------------------------------------------------------- /step10.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 17 | } 18 | 19 | public function doSomething() 20 | { 21 | $this->bim->doSomething(); 22 | echo __METHOD__, '|'; 23 | } 24 | } 25 | 26 | class Foo 27 | { 28 | private $bar; 29 | 30 | public function __construct(Bar $bar) 31 | { 32 | $this->bar = $bar; 33 | } 34 | 35 | public function doSomething() 36 | { 37 | $this->bar->doSomething(); 38 | echo __METHOD__; 39 | } 40 | } 41 | 42 | class Container 43 | { 44 | private $s = array(); 45 | 46 | public function __set($k, $c) 47 | { 48 | $this->s[$k] = $c; 49 | } 50 | 51 | public function __get($k) 52 | { 53 | // return $this->s[$k]($this); 54 | return $this->build($this->s[$k]); 55 | } 56 | 57 | public function getS () { 58 | return $this->s; 59 | } 60 | 61 | /** 62 | * 自动绑定(Autowiring)自动解析(Automatic Resolution) 63 | * 64 | * @param string $className 65 | * @return object 66 | * @throws Exception 67 | */ 68 | public function build($className, $flag = false) 69 | { 70 | echo "\n\n"; 71 | echo "--------------------处理{$className}--------------------------------------------------------------------------\n"; 72 | if ($flag) { 73 | echo "递归解析{$className}\n"; 74 | } else { 75 | echo "非递归解析{$className}\n"; 76 | } 77 | 78 | // 如果是匿名函数(Anonymous functions),也叫闭包函数(closures) 79 | echo "显示是否是instanceof Closure\n"; 80 | var_dump($className instanceof Closure); 81 | if ($className instanceof Closure) { 82 | // 执行闭包函数,并将结果 83 | return $className($this); 84 | } 85 | 86 | /** @var ReflectionClass $reflector */ 87 | $reflector = new ReflectionClass($className); 88 | echo "显示ReflectionClass\n"; 89 | var_dump($reflector); 90 | 91 | // 检查类是否可实例化, 排除抽象类abstract和对象接口interface 92 | echo "检查类是否可实例化, 排除抽象类abstract和对象接口interface\n"; 93 | var_dump($reflector->isInstantiable()); 94 | if (!$reflector->isInstantiable()) { 95 | throw new Exception("Can't instantiate this."); 96 | } 97 | 98 | /** @var ReflectionMethod $constructor 获取类的构造函数 */ 99 | $constructor = $reflector->getConstructor(); 100 | echo "获取类的构造函数\n"; 101 | var_dump($constructor); 102 | 103 | // 若无构造函数,直接实例化并返回 104 | if (is_null($constructor)) { 105 | echo "若无构造函数,直接实例化并返回\n"; 106 | $o = new $className; 107 | var_dump($o); 108 | return $o; 109 | } 110 | 111 | // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 112 | $parameters = $constructor->getParameters(); 113 | echo "取构造函数参数,通过 ReflectionParameter 数组返回参数列表\n"; 114 | var_dump($parameters); 115 | 116 | // 递归解析构造函数的参数 117 | echo "<<<<开始>>>>递归解析构造函数的参数\n"; 118 | $dependencies = $this->getDependencies($parameters); 119 | echo "递归结束\n"; 120 | var_dump($dependencies); 121 | 122 | // 创建一个类的新实例,给出的参数将传递到类的构造函数。 123 | echo "创建一个类的新实例,给出的参数将传递到类的构造函数\n"; 124 | $b = $reflector->newInstanceArgs($dependencies); 125 | var_dump($b); 126 | return $b; 127 | } 128 | 129 | /** 130 | * @param array $parameters 131 | * @return array 132 | * @throws Exception 133 | */ 134 | public function getDependencies($parameters) 135 | { 136 | echo "--------在方法getDependencies中--------\n"; 137 | echo "显示参数\n"; 138 | var_dump($parameters); 139 | $dependencies = []; 140 | 141 | /** @var ReflectionParameter $parameter */ 142 | echo "进入循环\n"; 143 | foreach ($parameters as $parameter) { 144 | var_dump($parameter); 145 | /** @var ReflectionClass $dependency */ 146 | $dependency = $parameter->getClass(); 147 | var_dump($dependency); 148 | var_dump(is_null($dependency)); 149 | if (is_null($dependency)) { 150 | // 是变量,有默认值则设置默认值 151 | echo "是变量,有默认值则设置默认值\n"; 152 | $dependencies[] = $this->resolveNonClass($parameter); 153 | } else { 154 | // 是一个类,递归解析 155 | echo "是一个类,递归解析\n"; 156 | $dependencies[] = $this->build($dependency->name, true); 157 | } 158 | } 159 | echo "方法getDependencies结束\n"; 160 | echo "返回值\n"; 161 | var_dump($dependencies); 162 | 163 | return $dependencies; 164 | } 165 | 166 | /** 167 | * @param ReflectionParameter $parameter 168 | * @return mixed 169 | * @throws Exception 170 | */ 171 | public function resolveNonClass($parameter) 172 | { 173 | // 有默认值则返回默认值 174 | if ($parameter->isDefaultValueAvailable()) { 175 | return $parameter->getDefaultValue(); 176 | } 177 | 178 | throw new Exception('I have no idea what to do here.'); 179 | } 180 | } 181 | 182 | // ---- 183 | $di = new Container(); 184 | 185 | $di->foo = 'Foo'; 186 | //print_r($di->getS()); 187 | /* 188 | * Array 189 | ( 190 | [foo] => Foo 191 | ) 192 | 193 | * 194 | */ 195 | 196 | /** @var Foo $foo */ 197 | $foo = $di->foo; 198 | echo "\n\n\n"; 199 | var_dump($foo); 200 | /* 201 | object(Foo)#5 (1) { 202 | ["bar":"Foo":private]=> 203 | object(Bar)#9 (1) { 204 | ["bim":"Bar":private]=> 205 | object(Bim)#11 (0) { 206 | } 207 | } 208 | } 209 | 210 | */ 211 | echo "整个处理结束\n"; 212 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 213 | echo "\n\n"; 214 | 215 | /* 216 | ubuntu@huzhi:~/webroot/www/MailEye/Shell$ php step10.php 217 | 218 | 219 | --------------------处理Foo-------------------------------------------------------------------------- 220 | 非递归解析Foo 221 | 显示是否是instanceof Closure 222 | bool(false) 223 | 显示ReflectionClass 224 | object(ReflectionClass)#2 (1) { 225 | ["name"]=> 226 | string(3) "Foo" 227 | } 228 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 229 | bool(true) 230 | 获取类的构造函数 231 | object(ReflectionMethod)#3 (2) { 232 | ["name"]=> 233 | string(11) "__construct" 234 | ["class"]=> 235 | string(3) "Foo" 236 | } 237 | 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 238 | array(1) { 239 | [0]=> 240 | &object(ReflectionParameter)#4 (1) { 241 | ["name"]=> 242 | string(3) "bar" 243 | } 244 | } 245 | <<<<开始>>>>递归解析构造函数的参数 246 | --------在方法getDependencies中-------- 247 | 显示参数 248 | array(1) { 249 | [0]=> 250 | &object(ReflectionParameter)#4 (1) { 251 | ["name"]=> 252 | string(3) "bar" 253 | } 254 | } 255 | 进入循环 256 | object(ReflectionParameter)#4 (1) { 257 | ["name"]=> 258 | string(3) "bar" 259 | } 260 | object(ReflectionClass)#5 (1) { 261 | ["name"]=> 262 | string(3) "Bar" 263 | } 264 | bool(false) 265 | 是一个类,递归解析 266 | 267 | 268 | --------------------处理Bar-------------------------------------------------------------------------- 269 | 递归解析Bar 270 | 显示是否是instanceof Closure 271 | bool(false) 272 | 显示ReflectionClass 273 | object(ReflectionClass)#6 (1) { 274 | ["name"]=> 275 | string(3) "Bar" 276 | } 277 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 278 | bool(true) 279 | 获取类的构造函数 280 | object(ReflectionMethod)#7 (2) { 281 | ["name"]=> 282 | string(11) "__construct" 283 | ["class"]=> 284 | string(3) "Bar" 285 | } 286 | 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 287 | array(1) { 288 | [0]=> 289 | &object(ReflectionParameter)#8 (1) { 290 | ["name"]=> 291 | string(3) "bim" 292 | } 293 | } 294 | <<<<开始>>>>递归解析构造函数的参数 295 | --------在方法getDependencies中-------- 296 | 显示参数 297 | array(1) { 298 | [0]=> 299 | &object(ReflectionParameter)#8 (1) { 300 | ["name"]=> 301 | string(3) "bim" 302 | } 303 | } 304 | 进入循环 305 | object(ReflectionParameter)#8 (1) { 306 | ["name"]=> 307 | string(3) "bim" 308 | } 309 | object(ReflectionClass)#9 (1) { 310 | ["name"]=> 311 | string(3) "Bim" 312 | } 313 | bool(false) 314 | 是一个类,递归解析 315 | 316 | 317 | --------------------处理Bim-------------------------------------------------------------------------- 318 | 递归解析Bim 319 | 显示是否是instanceof Closure 320 | bool(false) 321 | 显示ReflectionClass 322 | object(ReflectionClass)#10 (1) { 323 | ["name"]=> 324 | string(3) "Bim" 325 | } 326 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 327 | bool(true) 328 | 获取类的构造函数 329 | NULL 330 | 若无构造函数,直接实例化并返回 331 | object(Bim)#11 (0) { 332 | } 333 | 方法getDependencies结束 334 | 返回值 335 | array(1) { 336 | [0]=> 337 | object(Bim)#11 (0) { 338 | } 339 | } 340 | 递归结束 341 | array(1) { 342 | [0]=> 343 | object(Bim)#11 (0) { 344 | } 345 | } 346 | 创建一个类的新实例,给出的参数将传递到类的构造函数 347 | object(Bar)#9 (1) { 348 | ["bim":"Bar":private]=> 349 | object(Bim)#11 (0) { 350 | } 351 | } 352 | 方法getDependencies结束 353 | 返回值 354 | array(1) { 355 | [0]=> 356 | object(Bar)#9 (1) { 357 | ["bim":"Bar":private]=> 358 | object(Bim)#11 (0) { 359 | } 360 | } 361 | } 362 | 递归结束 363 | array(1) { 364 | [0]=> 365 | object(Bar)#9 (1) { 366 | ["bim":"Bar":private]=> 367 | object(Bim)#11 (0) { 368 | } 369 | } 370 | } 371 | 创建一个类的新实例,给出的参数将传递到类的构造函数 372 | object(Foo)#5 (1) { 373 | ["bar":"Foo":private]=> 374 | object(Bar)#9 (1) { 375 | ["bim":"Bar":private]=> 376 | object(Bim)#11 (0) { 377 | } 378 | } 379 | } 380 | 381 | 382 | 383 | object(Foo)#5 (1) { 384 | ["bar":"Foo":private]=> 385 | object(Bar)#9 (1) { 386 | ["bim":"Bar":private]=> 387 | object(Bim)#11 (0) { 388 | } 389 | } 390 | } 391 | 整个处理结束 392 | Bim::doSomething|Bar::doSomething|Foo::doSomething 393 | 394 | ubuntu@huzhi:~/webroot/www/MailEye/Shell$ 395 | */ -------------------------------------------------------------------------------- /step11.php: -------------------------------------------------------------------------------- 1 | bim = $bim; 17 | } 18 | 19 | public function doSomething() 20 | { 21 | $this->bim->doSomething(); 22 | echo __METHOD__, '|'; 23 | } 24 | } 25 | 26 | class Too 27 | { 28 | private $bim; 29 | 30 | public function __construct(Bim $bim) 31 | { 32 | $this->bim = $bim; 33 | } 34 | 35 | public function doSomething() 36 | { 37 | $this->bim->doSomething(); 38 | echo __METHOD__, '|'; 39 | } 40 | } 41 | 42 | class Foo 43 | { 44 | private $bar; 45 | private $too; 46 | 47 | public function __construct(Bar $bar, Too $too) 48 | { 49 | $this->bar = $bar; 50 | $this->too = $too; 51 | } 52 | 53 | public function doSomething() 54 | { 55 | $this->bar->doSomething(); 56 | $this->too->doSomething(); 57 | echo __METHOD__; 58 | } 59 | } 60 | 61 | class Container 62 | { 63 | private $s = array(); 64 | 65 | public function __set($k, $c) 66 | { 67 | $this->s[$k] = $c; 68 | } 69 | 70 | public function __get($k) 71 | { 72 | // return $this->s[$k]($this); 73 | return $this->build($this->s[$k]); 74 | } 75 | 76 | public function getS () { 77 | return $this->s; 78 | } 79 | 80 | /** 81 | * 自动绑定(Autowiring)自动解析(Automatic Resolution) 82 | * 83 | * @param string $className 84 | * @return object 85 | * @throws Exception 86 | */ 87 | public function build($className, $flag = false) 88 | { 89 | echo "\n\n"; 90 | echo "--------------------处理{$className}--------------------------------------------------------------------------\n"; 91 | if ($flag) { 92 | echo "递归解析{$className}\n"; 93 | } else { 94 | echo "非递归解析{$className}\n"; 95 | } 96 | 97 | // 如果是匿名函数(Anonymous functions),也叫闭包函数(closures) 98 | echo "显示是否是instanceof Closure\n"; 99 | var_dump($className instanceof Closure); 100 | if ($className instanceof Closure) { 101 | // 执行闭包函数,并将结果 102 | return $className($this); 103 | } 104 | 105 | /** @var ReflectionClass $reflector */ 106 | $reflector = new ReflectionClass($className); 107 | echo "显示ReflectionClass\n"; 108 | var_dump($reflector); 109 | 110 | // 检查类是否可实例化, 排除抽象类abstract和对象接口interface 111 | echo "检查类是否可实例化, 排除抽象类abstract和对象接口interface\n"; 112 | var_dump($reflector->isInstantiable()); 113 | if (!$reflector->isInstantiable()) { 114 | throw new Exception("Can't instantiate this."); 115 | } 116 | 117 | /** @var ReflectionMethod $constructor 获取类的构造函数 */ 118 | $constructor = $reflector->getConstructor(); 119 | echo "获取类的构造函数\n"; 120 | var_dump($constructor); 121 | 122 | // 若无构造函数,直接实例化并返回 123 | if (is_null($constructor)) { 124 | echo "若无构造函数,直接实例化并返回\n"; 125 | $o = new $className; 126 | var_dump($o); 127 | return $o; 128 | } 129 | 130 | // 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 131 | $parameters = $constructor->getParameters(); 132 | echo "取构造函数参数,通过 ReflectionParameter 数组返回参数列表\n"; 133 | var_dump($parameters); 134 | 135 | // 递归解析构造函数的参数 136 | echo "<<<<开始>>>>递归解析构造函数的参数\n"; 137 | $dependencies = $this->getDependencies($parameters); 138 | echo "递归结束\n"; 139 | var_dump($dependencies); 140 | 141 | // 创建一个类的新实例,给出的参数将传递到类的构造函数。 142 | echo "创建一个类的新实例,给出的参数将传递到类的构造函数\n"; 143 | $b = $reflector->newInstanceArgs($dependencies); 144 | var_dump($b); 145 | return $b; 146 | } 147 | 148 | /** 149 | * @param array $parameters 150 | * @return array 151 | * @throws Exception 152 | */ 153 | public function getDependencies($parameters) 154 | { 155 | echo "--------在方法getDependencies中--------\n"; 156 | echo "显示参数\n"; 157 | var_dump($parameters); 158 | $dependencies = []; 159 | 160 | /** @var ReflectionParameter $parameter */ 161 | foreach ($parameters as $parameter) { 162 | echo "进入循环\n"; 163 | var_dump($parameter); 164 | /** @var ReflectionClass $dependency */ 165 | $dependency = $parameter->getClass(); 166 | var_dump($dependency); 167 | var_dump(is_null($dependency)); 168 | if (is_null($dependency)) { 169 | // 是变量,有默认值则设置默认值 170 | echo "是变量,有默认值则设置默认值\n"; 171 | $dependencies[] = $this->resolveNonClass($parameter); 172 | } else { 173 | // 是一个类,递归解析 174 | echo "是一个类,递归解析\n"; 175 | $dependencies[] = $this->build($dependency->name, true); 176 | } 177 | } 178 | echo "方法getDependencies结束\n"; 179 | echo "返回值\n"; 180 | var_dump($dependencies); 181 | 182 | return $dependencies; 183 | } 184 | 185 | /** 186 | * @param ReflectionParameter $parameter 187 | * @return mixed 188 | * @throws Exception 189 | */ 190 | public function resolveNonClass($parameter) 191 | { 192 | // 有默认值则返回默认值 193 | if ($parameter->isDefaultValueAvailable()) { 194 | return $parameter->getDefaultValue(); 195 | } 196 | 197 | throw new Exception('I have no idea what to do here.'); 198 | } 199 | } 200 | 201 | // ---- 202 | $di = new Container(); 203 | 204 | $di->foo = 'Foo'; 205 | //print_r($di->getS()); 206 | /* 207 | * Array 208 | ( 209 | [foo] => Foo 210 | ) 211 | 212 | * 213 | */ 214 | 215 | /** @var Foo $foo */ 216 | $foo = $di->foo; 217 | echo "\n\n\n"; 218 | var_dump($foo); 219 | /* 220 | object(Foo)#5 (1) { 221 | ["bar":"Foo":private]=> 222 | object(Bar)#9 (1) { 223 | ["bim":"Bar":private]=> 224 | object(Bim)#11 (0) { 225 | } 226 | } 227 | } 228 | 229 | */ 230 | echo "整个处理结束\n"; 231 | $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething 232 | echo "\n\n"; 233 | 234 | /* 235 | ubuntu@huzhi:~/webroot/www/MailEye/Shell$ php step11.php 236 | 237 | 238 | --------------------处理Foo-------------------------------------------------------------------------- 239 | 非递归解析Foo 240 | 显示是否是instanceof Closure 241 | bool(false) 242 | 显示ReflectionClass 243 | object(ReflectionClass)#2 (1) { 244 | ["name"]=> 245 | string(3) "Foo" 246 | } 247 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 248 | bool(true) 249 | 获取类的构造函数 250 | object(ReflectionMethod)#3 (2) { 251 | ["name"]=> 252 | string(11) "__construct" 253 | ["class"]=> 254 | string(3) "Foo" 255 | } 256 | 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 257 | array(2) { 258 | [0]=> 259 | &object(ReflectionParameter)#4 (1) { 260 | ["name"]=> 261 | string(3) "bar" 262 | } 263 | [1]=> 264 | &object(ReflectionParameter)#5 (1) { 265 | ["name"]=> 266 | string(3) "too" 267 | } 268 | } 269 | <<<<开始>>>>递归解析构造函数的参数 270 | --------在方法getDependencies中-------- 271 | 显示参数 272 | array(2) { 273 | [0]=> 274 | &object(ReflectionParameter)#4 (1) { 275 | ["name"]=> 276 | string(3) "bar" 277 | } 278 | [1]=> 279 | &object(ReflectionParameter)#5 (1) { 280 | ["name"]=> 281 | string(3) "too" 282 | } 283 | } 284 | 进入循环 285 | object(ReflectionParameter)#4 (1) { 286 | ["name"]=> 287 | string(3) "bar" 288 | } 289 | object(ReflectionClass)#6 (1) { 290 | ["name"]=> 291 | string(3) "Bar" 292 | } 293 | bool(false) 294 | 是一个类,递归解析 295 | 296 | 297 | --------------------处理Bar-------------------------------------------------------------------------- 298 | 递归解析Bar 299 | 显示是否是instanceof Closure 300 | bool(false) 301 | 显示ReflectionClass 302 | object(ReflectionClass)#7 (1) { 303 | ["name"]=> 304 | string(3) "Bar" 305 | } 306 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 307 | bool(true) 308 | 获取类的构造函数 309 | object(ReflectionMethod)#8 (2) { 310 | ["name"]=> 311 | string(11) "__construct" 312 | ["class"]=> 313 | string(3) "Bar" 314 | } 315 | 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 316 | array(1) { 317 | [0]=> 318 | &object(ReflectionParameter)#9 (1) { 319 | ["name"]=> 320 | string(3) "bim" 321 | } 322 | } 323 | <<<<开始>>>>递归解析构造函数的参数 324 | --------在方法getDependencies中-------- 325 | 显示参数 326 | array(1) { 327 | [0]=> 328 | &object(ReflectionParameter)#9 (1) { 329 | ["name"]=> 330 | string(3) "bim" 331 | } 332 | } 333 | 进入循环 334 | object(ReflectionParameter)#9 (1) { 335 | ["name"]=> 336 | string(3) "bim" 337 | } 338 | object(ReflectionClass)#10 (1) { 339 | ["name"]=> 340 | string(3) "Bim" 341 | } 342 | bool(false) 343 | 是一个类,递归解析 344 | 345 | 346 | --------------------处理Bim-------------------------------------------------------------------------- 347 | 递归解析Bim 348 | 显示是否是instanceof Closure 349 | bool(false) 350 | 显示ReflectionClass 351 | object(ReflectionClass)#11 (1) { 352 | ["name"]=> 353 | string(3) "Bim" 354 | } 355 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 356 | bool(true) 357 | 获取类的构造函数 358 | NULL 359 | 若无构造函数,直接实例化并返回 360 | object(Bim)#12 (0) { 361 | } 362 | 方法getDependencies结束 363 | 返回值 364 | array(1) { 365 | [0]=> 366 | object(Bim)#12 (0) { 367 | } 368 | } 369 | 递归结束 370 | array(1) { 371 | [0]=> 372 | object(Bim)#12 (0) { 373 | } 374 | } 375 | 创建一个类的新实例,给出的参数将传递到类的构造函数 376 | object(Bar)#10 (1) { 377 | ["bim":"Bar":private]=> 378 | object(Bim)#12 (0) { 379 | } 380 | } 381 | 进入循环 382 | object(ReflectionParameter)#5 (1) { 383 | ["name"]=> 384 | string(3) "too" 385 | } 386 | object(ReflectionClass)#9 (1) { 387 | ["name"]=> 388 | string(3) "Too" 389 | } 390 | bool(false) 391 | 是一个类,递归解析 392 | 393 | 394 | --------------------处理Too-------------------------------------------------------------------------- 395 | 递归解析Too 396 | 显示是否是instanceof Closure 397 | bool(false) 398 | 显示ReflectionClass 399 | object(ReflectionClass)#6 (1) { 400 | ["name"]=> 401 | string(3) "Too" 402 | } 403 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 404 | bool(true) 405 | 获取类的构造函数 406 | object(ReflectionMethod)#8 (2) { 407 | ["name"]=> 408 | string(11) "__construct" 409 | ["class"]=> 410 | string(3) "Too" 411 | } 412 | 取构造函数参数,通过 ReflectionParameter 数组返回参数列表 413 | array(1) { 414 | [0]=> 415 | &object(ReflectionParameter)#7 (1) { 416 | ["name"]=> 417 | string(3) "bim" 418 | } 419 | } 420 | <<<<开始>>>>递归解析构造函数的参数 421 | --------在方法getDependencies中-------- 422 | 显示参数 423 | array(1) { 424 | [0]=> 425 | &object(ReflectionParameter)#7 (1) { 426 | ["name"]=> 427 | string(3) "bim" 428 | } 429 | } 430 | 进入循环 431 | object(ReflectionParameter)#7 (1) { 432 | ["name"]=> 433 | string(3) "bim" 434 | } 435 | object(ReflectionClass)#11 (1) { 436 | ["name"]=> 437 | string(3) "Bim" 438 | } 439 | bool(false) 440 | 是一个类,递归解析 441 | 442 | 443 | --------------------处理Bim-------------------------------------------------------------------------- 444 | 递归解析Bim 445 | 显示是否是instanceof Closure 446 | bool(false) 447 | 显示ReflectionClass 448 | object(ReflectionClass)#13 (1) { 449 | ["name"]=> 450 | string(3) "Bim" 451 | } 452 | 检查类是否可实例化, 排除抽象类abstract和对象接口interface 453 | bool(true) 454 | 获取类的构造函数 455 | NULL 456 | 若无构造函数,直接实例化并返回 457 | object(Bim)#14 (0) { 458 | } 459 | 方法getDependencies结束 460 | 返回值 461 | array(1) { 462 | [0]=> 463 | object(Bim)#14 (0) { 464 | } 465 | } 466 | 递归结束 467 | array(1) { 468 | [0]=> 469 | object(Bim)#14 (0) { 470 | } 471 | } 472 | 创建一个类的新实例,给出的参数将传递到类的构造函数 473 | object(Too)#11 (1) { 474 | ["bim":"Too":private]=> 475 | object(Bim)#14 (0) { 476 | } 477 | } 478 | 方法getDependencies结束 479 | 返回值 480 | array(2) { 481 | [0]=> 482 | object(Bar)#10 (1) { 483 | ["bim":"Bar":private]=> 484 | object(Bim)#12 (0) { 485 | } 486 | } 487 | [1]=> 488 | object(Too)#11 (1) { 489 | ["bim":"Too":private]=> 490 | object(Bim)#14 (0) { 491 | } 492 | } 493 | } 494 | 递归结束 495 | array(2) { 496 | [0]=> 497 | object(Bar)#10 (1) { 498 | ["bim":"Bar":private]=> 499 | object(Bim)#12 (0) { 500 | } 501 | } 502 | [1]=> 503 | object(Too)#11 (1) { 504 | ["bim":"Too":private]=> 505 | object(Bim)#14 (0) { 506 | } 507 | } 508 | } 509 | 创建一个类的新实例,给出的参数将传递到类的构造函数 510 | object(Foo)#9 (2) { 511 | ["bar":"Foo":private]=> 512 | object(Bar)#10 (1) { 513 | ["bim":"Bar":private]=> 514 | object(Bim)#12 (0) { 515 | } 516 | } 517 | ["too":"Foo":private]=> 518 | object(Too)#11 (1) { 519 | ["bim":"Too":private]=> 520 | object(Bim)#14 (0) { 521 | } 522 | } 523 | } 524 | 525 | 526 | 527 | object(Foo)#9 (2) { 528 | ["bar":"Foo":private]=> 529 | object(Bar)#10 (1) { 530 | ["bim":"Bar":private]=> 531 | object(Bim)#12 (0) { 532 | } 533 | } 534 | ["too":"Foo":private]=> 535 | object(Too)#11 (1) { 536 | ["bim":"Too":private]=> 537 | object(Bim)#14 (0) { 538 | } 539 | } 540 | } 541 | 整个处理结束 542 | Bim::doSomething|Bar::doSomething|Bim::doSomething|Too::doSomething|Foo::doSomething 543 | 544 | ubuntu@huzhi:~/webroot/www/MailEye/Shell$ 545 | */ 546 | --------------------------------------------------------------------------------