• 一个手写的依赖注入的实现代码


        $reflection = new ReflectionClass(AppServicesEmailService::class);
    
            //获取构造方法的对象
            $constructor = $reflection->getConstructor();
    
            //获取构造方法的参数列表
            $parameters = $constructor->getParameters();
    
            $newParams = [];
    
            //遍历参数列表
            foreach ($parameters as $params) {
                // 获取对象
                $dependency = $params->getClass();
    
                //如果为null
                if (is_null($dependency)) {
                    if ($params->isDefaultValueAvailable()) {
                        $newParams[] = $params->getDefaultValue();
                    } else {
                        throw new Exception('Can not resolve class dependency {$parameter->name}');
                    }
                //不为null则实例化放入数组
                } else {
                    $class = $dependency->name;
                    $newParams[] = new $class;
                }
            }
    
            $service = $reflection->newInstanceArgs($newParams);
            //调用注入类的方法即可    
  • 相关阅读:
    _proto_和prototype的区别
    ajax
    图片预加载
    loading动画
    WinSCP
    检测竖屏 横屏
    webstrom hbuilder快捷键
    vue 引入sass
    npm install -save 和 -save-dev 区别
    打乱数组顺序
  • 原文地址:https://www.cnblogs.com/tudou1223/p/11206819.html
Copyright © 2020-2023  润新知