首页 > 其他 > 详细

每天laravel-20160804| Container -7

时间:2016-05-11 11:31:34      阅读:157      评论:0      收藏:0      [点我收藏+]
 /**
  * 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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!