DI设计与实现
方法 | 要求 |
createObject($config) | $config中必须有class节点,表示对象的类型 |
set($name,$config) | $config放入容器 |
get($name) | 如果$name已经被实例化,直接返回,若未被实例化,则实例化放入容器并返回 |
insure($config,$defaultClass='') | 动态组建 |
咱们一起来看一下yaf的Registry这个类,这是yaf代码提示中自带的类。
<?php namespace Yaf ; /** * <p>All methods of <b>\Yaf\Registry</b> declared as static, making it universally accessible. This provides the ability to get or set any custom data from anyway in your code as necessary.</p> * @link http://www.php.net/manual/en/class.yaf-registry.php */ final class Registry { /** * @var \Yaf\Registry */ protected static $_instance; /** * @var array */ protected $_entries; /** * @link http://www.php.net/manual/en/yaf-registry.construct.php */ private function __construct(){ } /** * @link http://www.php.net/manual/en/yaf-registry.clone.php */ private function __clone(){ } /** * Retrieve an item from registry * * @link http://www.php.net/manual/en/yaf-registry.get.php * * @param string $name * * @return mixed */ public static function get($name){ } /** * Check whether an item exists * * @link http://www.php.net/manual/en/yaf-registry.has.php * * @param string $name * * @return bool */ public static function has($name){ } /** * @link http://www.php.net/manual/en/yaf-registry.set.php * * @param string $name * @param mixed $value * * @return bool */ public static function set($name, $value){ } /** * @link http://www.php.net/manual/en/yaf-registry.del.php * * @param string $name * * @return void|bool */ public static function del($name){ } }
下面咱们根据Registry这个类,创建实际项目中可以用到的DI容器
/** * Class ${小痴} * @package extend */ class Di { private function __construct() { } private function __clone() { // TODO: Implement __clone() method. } /** * Notes:块赋值 * User: 小痴 * Date: 2019/3/18 * Time: 13:17 * @return void */ public static function block($object,$properties=[]) { foreach ($properties as $property => $value) { $object->$property = $value; } } /** * Notes:根据对象配置创建对象 * User: 小痴 * Date: 2019/3/18 * Time: 13:16 * @return mixed */ public static function createObject($config) { $class = $config['class']; unset($config['class']); $object = new $class(); self::block($object,$config); if(method_exists($object,'init')) { $object->init(); } return $object; } /** * Notes: * User: 小痴 * Date: 2019/3/18 * Time: 13:17 * @return bool */ public static function set($name,$config) { return Registry::set($name,$config); } /** * Notes: * User: 小痴 * Date: 2019/3/18 * Time: 13:17 * @return mixed */ public static function get($name) { $object = Registry::get($name); if(is_array($object) && isset($object['class'])) { $instance = self::createObject($object); Registry::set($name,$instance); return $instance; } return $object; } /** * Notes: * User: 小痴 * Date: 2019/3/18 * Time: 13:17 * @return array|mixed */ public static function insure($config,$defaultClass=null) { if(is_array($config)) { if(isset($config['class'])) { return self::createObject($config); } if(isset($defaultClass)) { $config['class'] = $defaultClass; return self::createObject($config); } } return $config; } }
举例:
在yaf中会有一个Bootstrap.php,在此文件中创建一个方法:
//获取一下DB
public function _initDb(\yaf\Dispatcher $dispatcher)
{
$config = \Yaf\Application::$app()->getConfig();
\Yaf\Registry::set('config',$config);
扫描二维码关注公众号,回复:
5578233 查看本文章
}
我们在controller中创建一个testDb.php文件:
public function testAction()
{
$db = Di::get('config');
var_dumo($db);
}
这样就获取到了。