php di 使用,PHP - DI 的使用

PHP - DI 的使用

DI 是什么.这里不在陈述.百度,谷歌上有很多资料

这里使用开源的DI组件库 php-DI

DI库开箱即用版本

确保你的电脑安装了composer.composer require php-di/php-diindex.php

require('vendor/autoload.php');

use DI\ContainerBuilder;

$builder = new ContainerBuilder();

$builder->addDefinitions(__DIR__ . '/config.php');

$container = $builder->build();

$proxy = $container->get('tt');

$proxy->doSomething();config.php

require 'test.class.php';

return [

tt::class => function () {

return new test();

},

tt1::class => function () {

var_dump(333);

return new test();

}

]test.class.php;

class test {

public function dosomething(){

var_dump(123123);

}

}

?>

解释:$builder->addDefinitions(__DIR__ . '/config.php'); //config.php 设置需要注入的类

$container = $builder->build();

$proxy = $container->get('tt'); //获取注入的类.ps: 使用的是别名

$proxy 就是test.class.php 的实例化的对象. 可以自行用 instanceof

if ( $proxy instanceof new test.class.php ) { } 去试试.但是记得引入test.class.php

有的人可能会问.这一个一个的require 不要疯掉..

接下来. 整合到实际项目中..在你的单入口处 引入容器require_once "kingphp/di/DI.class.php"; // DI容器DI.class.php

use DI\ContainerBuilder;

/**

* DI::get('log')->addError('error');

* DI::get('event')->emit('user.refer',array(['roomid'=>'1']));

*/

class DI {

private static $dibuild = '';

// 自动加载

public static function autoload( $class_name )

{

//自动加载类文件的方法.一些简单的判断.文件是否存在,类是否存在.可自行自定义.也可以使用comonser 的autoload.php 可自行百度关键词.composer加载自己的类.![1486449923.png](http://oizlltfdh.bkt.clouddn.com/2017/02/3490788300.png)

$class_path = str_replace("\\",DIRECTORY_SEPARATOR,$class_name);

if( empty( $class_path ) || !is_file( $class_path ) )

{

$class_path = $class_path.'.php';

}

if ( is_file($class_path) ) {

require_once ($class_path);

if(class_exists($class_name,false)){

return true;

}

}

return false;

}

/**

* 创建DI 对象

*/

public static function create() {

if( empty(self::$dibuild) ) {

spl_autoload_register('self::autoload');

$builder = new ContainerBuilder();

$builder->addDefinitions(__DIR__ . '/config.php');

$container = $builder->build();

self::$dibuild = $container;

}

return self::$dibuild;

}

/**

* 获取DI 对象

*/

public static function get( $className = '' ) {

$di = self::create();

return $di->get( $className );

}

}

?>config.php

use kingphp\event\listener;

use Monolog\Logger;

use Monolog\Handler\StreamHandler;

use Monolog\Formatter\LineFormatter;

return [

event::class => function () {

return listener::create();

},

log::class =>function () {

$log = new Logger('name');

$formatter = new LineFormatter();

$sterm = new StreamHandler('var/log/mongolog.log', Logger::DEBUG);

$sterm->setFormatter($formatter);

$log->pushHandler($sterm);

return $log;

}

]

?>

然后你就可以在你的MVC中到处使用 DI::get('log')->addError('error');了

解释:注入类到 '容器' 中是在config.php文件中.

我这里注入的时候自己写了一个自动加载类使用的是spl_autoload_register();

获取容器中的类 DI::get('log')->addError('error');

本文由 舒舒 创作,采用 知识共享署名4.0 国际许可协议进行许可

本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名

最后编辑时间为: Feb 9, 2017 at 10:20 am

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值