1<?PHP2 3 //A container class-loaded instance or a callback function that provides an instance4 classContainer {5 6 protected $bindings= [];7 8 //binding interfaces and generating callback functions for corresponding instances9 Public functionBind$abstract,$concrete=NULL,$shared=false) {Ten One //if the supplied parameter is not a callback function, the default callback function is generated A if(!$concreteinstanceof Closure) { - $concrete=$this->getclosure ($abstract,$concrete); - } the - $this->bindings[$abstract] =Compact(' concrete ', ' shared '); - } - + //callback function for the default build instance - protected functionGetclosure ($abstract,$concrete) { + A return function($c) Use($abstract,$concrete) { at $method= ($abstract==$concrete) ? ' Build ': ' Make '; - return $c-$method($concrete); - }; - - } - in Public functionMake$abstract) { - $concrete=$this->getconcrete ($abstract); to + if($this->isbuildable ($concrete,$abstract)) { - $object=$this->build ($concrete); the}Else { * $object=$this->make ($concrete); $ }Panax Notoginseng - return $object; the } + A protected functionIsbuildable ($concrete,$abstract) { the return $concrete===$abstract||$concreteinstanceof Closure; + } - $ //gets the callback function for the binding $ protected functionGetconcrete ($abstract) { - if(!isset($this->bindings[$abstract])) { - return $abstract; the } - Wuyi return $this->bindings[$abstract[' Concrete ']; the } - Wu //instantiating an object - Public functionBuild$concrete) { About $ if($concreteinstanceof Closure) { - return $concrete($this); - } - A $reflector=NewReflectionclass ($concrete); + if(!$reflector-isinstantiable ()) { the Echo $message= "Target [$concrete] is not instantiable "; - } $ the $constructor=$reflector-GetConstructor (); the if(Is_null($constructor)) { the return New $concrete; the } - in $dependencies=$constructor-getparameters (); the $instances=$this->getdependencies ($dependencies); the About return $reflector->newinstanceargs ($instances); the } the the //resolving dependencies when instantiating objects through a reflection mechanism + protected functionGetdependencies ($parameters) { - $dependencies= []; the foreach($parameters as $parameter) {Bayi $dependency=$parameter-getclass (); the if(Is_null($dependency)) { the $dependencies[] =NULL; -}Else { - $dependencies[] =$this->resolveclass ($parameter); the } the } the the return(Array)$dependencies; - } the the protected functionResolveclass (Reflectionparameter$parameter) { the return $this->make ($parameter->getclass ()name);94 } the the } the 98 About classTraveller { - 101 protected $trafficTool;102 103 Public function__construct (Visit$trafficTool) {104 $this->traffictool =$trafficTool; the }106 107 Public functionVisittibet () {108 $this->traffictool->go ();109 } the 111 } the 113 InterfaceVisit { the Public functiongo (); the } the 117 classTrainImplementsVisit {118 119 Public functionGo () { - Echo"Go to Tibet by train!!!";121 }122 123 }124 the $app=NewContainer ();126 $app->bind ("Visit", "Train");127 $app->bind ("Traveller", "Traveller"); - 129 the $tra=$app->make ("Traveller");131 $tra->visittibet ();
Simplified code for container classes in the Laravel framework-excerpted from a book