容器类调用make方法时,如果没有已注册的key,那么会自动通过反射类实例化具体类

make

    /**
* Resolve the given type from the container.
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function make($abstract, array $parameters = [])
{
$abstract = $this->getAlias($abstract); // If an instance of the type is currently being managed as a singleton we'll
// just return an existing instance instead of instantiating new instances
// so the developer can keep using the same objects instance every time.
if (isset($this->instances[$abstract])) {
return $this->instances[$abstract];
} $concrete = $this->getConcrete($abstract); // We're ready to instantiate an instance of the concrete type registered for
// the binding. This will instantiate the types, as well as resolve any of
// its "nested" dependencies recursively until all have gotten resolved.
if ($this->isBuildable($concrete, $abstract)) {
$object = $this->build($concrete, $parameters);
} else {
$object = $this->make($concrete, $parameters);
} // If we defined any extenders for this type, we'll need to spin through them
// and apply them to the object being built. This allows for the extension
// of services, such as changing configuration or decorating the object.
foreach ($this->getExtenders($abstract) as $extender) {
$object = $extender($object, $this);
} // If the requested type is registered as a singleton we'll want to cache off
// the instances in "memory" so we can return it later without creating an
// entirely new instance of an object on each subsequent request for it.
if ($this->isShared($abstract)) {
$this->instances[$abstract] = $object;
} $this->fireResolvingCallbacks($abstract, $object); $this->resolved[$abstract] = true; return $object;
}

getConcrete

    /**
* Get the concrete type for a given abstract.
*
* @param string $abstract
* @return mixed $concrete
*/
protected function getConcrete($abstract)
{
if (! is_null($concrete = $this->getContextualConcrete($abstract))) {
return $concrete;
} // If we don't have a registered resolver or concrete for the type, we'll just
// assume each type is a concrete name and will attempt to resolve it as is
// since the container should be able to resolve concretes automatically.
if (! isset($this->bindings[$abstract])) {
if ($this->missingLeadingSlash($abstract) &&
isset($this->bindings['\\'.$abstract])) {
$abstract = '\\'.$abstract;
} return $abstract;
} return $this->bindings[$abstract]['concrete'];
}

build

    /**
* Instantiate a concrete instance of the given type.
*
* @param string $concrete
* @param array $parameters
* @return mixed
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function build($concrete, array $parameters = [])
{
// If the concrete type is actually a Closure, we will just execute it and
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this, $parameters);
} $reflector = new ReflectionClass($concrete); // If the type is not instantiable, the developer is attempting to resolve
// an abstract type such as an Interface of Abstract Class and there is
// no binding registered for the abstractions so we need to bail out.
if (! $reflector->isInstantiable()) {
$message = "Target [$concrete] is not instantiable."; throw new BindingResolutionContractException($message);
} $this->buildStack[] = $concrete; $constructor = $reflector->getConstructor(); // If there are no constructors, that means there are no dependencies then
// we can just resolve the instances of the objects right away, without
// resolving any other types or dependencies out of these containers.
if (is_null($constructor)) {
array_pop($this->buildStack); return new $concrete;
} $dependencies = $constructor->getParameters(); // Once we have all the constructor's parameters we can create each of the
// dependency instances and then use the reflection instances to make a
// new instance of this class, injecting the created dependencies in.
$parameters = $this->keyParametersByArgument(
$dependencies, $parameters
); $instances = $this->getDependencies(
$dependencies, $parameters
); array_pop($this->buildStack); return $reflector->newInstanceArgs($instances);
}

laravel容器类make方法解释的更多相关文章

  1. Info.plist和pch文件的作用,UIApplication,iOS程序的启动过程,AppDelegate 方法解释,UIWindow,生命周期方法

    Info.plist常见的设置 建立一个工程后,会在Supporting files文件夹下看到一个“工程名-Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 注:在旧 ...

  2. es6的map()方法解释

    es6的map()方法解释   map方法的作用不难理解,即“映射”,也就是原数组被“映射”成对应新数组.下面这个例子是数值项求平方: var data = [1, 2, 3, 4]; var arr ...

  3. Laravel Route Resource 方法

    新增的 resource 方法将遵从 RESTful 架构为用户资源生成路由.该方法接收两个参数,第一个参数为资源名称,第二个参数为控制器名称. Route::resource('users', 'U ...

  4. laravel 自定义常量方法

    laravel 自定义常量方法 版本5以上验证OK 常量定义方案A: step1 按路径生成constants文件app/config/constants.php step2 constants文件里 ...

  5. vue第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件)

    第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件) #课程目标 掌握vue实例的相关属性和方法的含义和使用 了解vue的数据响应原理 熟悉创建组件,了解全局 ...

  6. Laravel [1045] 解决方法 Access denied for user 'homestead'@'localhost'

    这几天学习Laravel框架遇到了数据库方面的问题. PDOException in Connector.php line 55:SQLSTATE[HY000] [1045] Access denie ...

  7. laravel框架少见方法详解

    1.whereDate() 方法 $q->where('created_at', '>=', date('Y-m-d').' 00:00:00')); 以前查数据时,直接用where条件来 ...

  8. laravel多种安装方法

    首先请确保环境为 PHP >= 5.5.9 OpenSSL PHP 扩展 PDO PHP 扩展 Mbstring PHP 扩展 Tokenizer PHP 扩展 方法一: 直接下载安装好的lar ...

  9. laravel中with()方法,has()方法和whereHas()方法的区别

    with() with()方法是用作"渴求式加载"的,那主要意味着,laravel将会伴随着主要模型预加载出确切的的关联关系.这就对那些如果你想加在一个模型的所有关联关系非常有帮助 ...

随机推荐

  1. 负载均衡集群(LBC)

    一.LVS简介及工作模式1. LVS简介Linux Virtual Server,该软件的功能是实现LB(load balance) 2.LVS的三种工作模式 1)NAT模式(NAT) LVS 服务器 ...

  2. P2P system: Napster

    Napster structure client machines之所以叫peers是因为对于server来说这些machines是平等对待的 当你upload一首歌曲如PennyLane.mp3时, ...

  3. vue 配置 TinyMCE

    1.index.html 增加cdn 地址 <script src="//cdn.bootcss.com/tinymce/5.0.16/tinymce.min.js"> ...

  4. Java知识点汇总-1

    目录 1 native方法 2 泛型 3 hashcode 4 JDK主要的包 5 被final修饰的类特点 6 空串与null串 7 判断字符串是否相等 1 native方法 JDK源代码由C++. ...

  5. 使用VS创建三层架构的项目

    使用VS创建三层架构的项目 1.打开VS软件: 2.创建新项目: 3.创建新项目打开后选择SAP的选项点击创建{注:上面还有一个和这个相似的项目创建:看清楚在选择: 4.创建后改下名字,框架如果不是3 ...

  6. luogu 3248

    直接向原树加子树是不可能的考虑重新建立这样一颗树,我们称之为 S 树 将每次需要添加的子树看做一个点,称之为 S 点 新建的树就是由这些点构成的,那么树的大小是合理的 初始节点为整棵原树由于添加的子树 ...

  7. luogu 1369

    给出平面上的n个点,请找出一个边与坐标轴平行的矩形,使得它的边界上有尽量多的点 模拟退火题解$n^2$ 处理每行的前缀和与每列的前缀和退火50次即可 #include <bits/stdc++. ...

  8. centos7 浏览器(firefox)中文乱码

    主要问题在于firefox用了系统没有的字体 百度的方案: 通过yum安装中文字体 (找不到对应的库) 修改firefox的默认字体(尴尬.不知道改哪个才有效) 粗暴的解决方案: 把 windows ...

  9. 数据层面;MySQL查

    AND 运算优先于OR运算执行(通过括号进行强化) count(*) 会得到包含NULL的数据行数:count(<列明>)会得到NULL之外的数据行数 SQL语句的总逻辑:书写顺序 sel ...

  10. 1卸载ROS

    sudo apt-get purge ros-*sudo rm -rf /etc/rosgedit ~/.bashrc 参考: https://blog.csdn.net/xmy306538517/a ...