国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP依賴倒置(Dependency Injection)代碼實(shí)例_PHP

php中文網(wǎng)
發(fā)布: 2016-05-31 19:29:36
原創(chuàng)
894人瀏覽過(guò)

實(shí)現(xiàn)類(lèi):

代碼如下:


?
class Container
{
??? protected $setings = array();
?
??? public function set($abstract, $concrete = null)
??? {
??????? if ($concrete === null) {
??????????? $concrete = $abstract;
??????? }
?
??????? $this->setings[$abstract] = $concrete;
??? }
?
??? public function get($abstract, $parameters = array())
??? {
??????? if (!isset($this->setings[$abstract])) {
??????????? return null;
??????? }
?
??????? return $this->build($this->setings[$abstract], $parameters);
??? }
?
??? public function build($concrete, $parameters)
??? {
??????? if ($concrete instanceof Closure) {
??????????? return $concrete($this, $parameters);
??????? }
?
??????? $reflector = new ReflectionClass($concrete);
?
??????? if (!$reflector->isInstantiable()) {
??????????? throw new Exception("Class {$concrete} is not instantiable");
??????? }
?
??????? $constructor = $reflector->getConstructor();
?
??????? if (is_null($constructor)) {
??????????? return $reflector->newInstance();
??????? }
?
??????? $parameters = $constructor->getParameters();
??????? $dependencies = $this->getDependencies($parameters);
?
??????? return $reflector->newInstanceArgs($dependencies);
??? }
?
??? public function getDependencies($parameters)
??? {
??????? $dependencies = array();
??????? foreach ($parameters as $parameter) {
??????????? $dependency = $parameter->getClass();
??????????? if ($dependency === null) {
??????????????? if ($parameter->isDefaultValueAvailable()) {
??????????????????? $dependencies[] = $parameter->getDefaultValue();
??????????????? } else {
??????????????????? throw new Exception("Can not be resolve class dependency {$parameter->name}");
??????????????? }
??????????? } else {
??????????????? $dependencies[] = $this->get($dependency->name);
??????????? }
??????? }
?
??????? return $dependencies;
??? }
}

實(shí)現(xiàn)實(shí)例:

代碼如下:


?
require 'container.php';
?
?
interface MyInterface{}
class Foo implements MyInterface{}
class Bar implements MyInterface{}
class Baz
{
??? public function __construct(MyInterface $foo)
??? {
??????? $this->foo = $foo;
??? }
}
?
$container = new Container();
$container->set('Baz', 'Baz');
$container->set('MyInterface', 'Foo');
$baz = $container->get('Baz');
print_r($baz);
$container->set('MyInterface', 'Bar');
$baz = $container->get('Baz');
print_r($baz);

PHP速學(xué)教程(入門(mén)到精通)
PHP速學(xué)教程(入門(mén)到精通)

PHP怎么學(xué)習(xí)?PHP怎么入門(mén)?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門(mén)到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!

下載
來(lái)源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn
最新問(wèn)題
開(kāi)源免費(fèi)商場(chǎng)系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)