码迷,mamicode.com
首页 > 其他好文 > 详细

每天laravel-20160804| Container -7

时间:2016-05-11 11:32:32      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:laravel

 /**
  * Wrap a Closure such that it is shared.
  *
  * @param  \Closure  $closure
  * @return \Closure
  */
 public function share(Closure $closure)// because this so we need php version is 5.4+
 {// Wrap a closure such that is shared
     return function ($container) use ($closure) {
         // We‘ll simply declare a static variable within the Closures and if it has
         // not been set we will execute the given Closures to resolve this value
         // and return it back to these consumers of the method as an instance.
         static $object;// check has, or closure it.

         if (is_null($object)) {
             $object = $closure($container);// use a closure class to bind to the container
         }

         return $object;
     };// a Closure class is a special class
 }

 /**
  * "Extend" an abstract type in the container.
  *
  * @param  string    $abstract
  * @param  \Closure  $closure
  * @return void
  *
  * @throws \InvalidArgumentException
  */
 public function extend($abstract, Closure $closure)// a extend type ,parameter is a abstract class,and other is closure.
 {
     $abstract = $this->normalize($abstract);// get  a normal string that we need

     if (isset($this->instances[$abstract])) {// check it is set in the instance array,
         $this->instances[$abstract] = $closure($this->instances[$abstract], $this);
// if isset ,use the closure function to action about it.
         $this->rebound($abstract);// get the rebound function
     } else {
         $this->extenders[$abstract][] = $closure;// set the extender
     }
 }


本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1772050

每天laravel-20160804| Container -7

标签:laravel

原文地址:http://jingshanls.blog.51cto.com/3357095/1772050

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!