Laravel depends on injection

month = $mouth; $this->hand = $hand;} public function attack() {$this->hand->scrap();} public function talk() {$this->month- >say(); }}class Mouth{ public function say(){ echo "咿咿呀呀"; }}class Hand{ public $finger; public function __construct(Finger $finger) {$this->finger = $finger; } public function scrap(){ echo'still toy';} public function otherFunction(){ $this->finger->eatFinger(); }}class Finger{ public function eatFinger(){ echo "Eat Finger"; }} class Container{ /** * Container binding, used to install the provided instance or provide the callback function of the instance* @var array */ public $building = []; /** * Register a binding to the container*/ public function bind($abstract, $concrete = nu ll, $shared = false){ if(is_null($concrete)){ $concrete = $abstract;} if(!$concrete instanceOf Closure){ $concrete = $this->getClosure($abstract, $concrete);} $this->building[$abstract] = compact("concrete", "shared");} //Register a shared binding singleton public function singleton($abstract, $concrete, $shared = true){ $this ->bind($abstract, $concrete, $shared);} /** * The callback closure of the default generated instance* * @param $abstract * @param $concrete * @return Closure */ public function getClosure($abstract, $concrete){ return function($c) use($abstract, $concrete){ $method = ($abstract == $concrete)?'build':'make'; return $c->$method($concrete) ; };} /** * Generate instance*/ public function make($abstract){ $concrete = $this->getConcrete($abstract); if($this->isBuildable($concrete, $abstract)){ $ object = $this->build($concrete); }else{ $object = $this->make($concrete);} return $object;} /** * Get the bound callback function */ public function getConcrete($abstract){ if(! isset($this->building[$abstract])){ return $abstract;} return $this->building[$abstract]['concrete'];} / ** * Determine whether a service entity can be created*/ public function isBuildable($concrete, $abstract){ return $concrete === $abstract || $concrete instanceof Closure;} /** * Instance specific objects according to the specific names of the instances* / public function build($concrete){ if($concrete instanceof Closure){ return $concrete($this);} //Create a reflection object $reflector = new ReflectionClass($concrete);//Get the class name through reflection if( ! $reflector->isInstantiable()){ //Throw an exception throw new \Exception('Unable to instantiate');} $constructor = $reflector->getConstructor();//Get the constructor if(is_null($constructor )){ return new $concrete;} $dependencies = $constructor->getParameters();//Get constructor parameters $instance = $this->getDependencies($dependencies); return $reflector->newInstanceArgs($instance); //Inject parameters into the object) //Resolve parameter dependencies through reflection public function getDependencies(array $dependencies){ $results = []; foreach( $dependencies as $dependency ){ $results[] = is_null($dependency->getClass()) ?$this->resolvedNonClass($dependency) :$this->resolvedClass ($dependency);} return $results;} //Resolve a dependency without type hint public function resolvedNonClass(ReflectionParameter $parameter) {if($parameter->isDefaultValueAvailable()){ return $parameter->getDefaultValue();} throw new \Exception('error');} //Resolve dependencies through the container public function resolvedClass(ReflectionParameter $parameter) {return $this->make($parameter->getClass()->name); })//Instantiate Container class $container = new Container(); $container->bind('Animal','People');//Put the class that needs to create the object into the container $people = $container->make('Animal') ;//Create object $people->attack();//Call method $people->talk();

Leave a Comment

Your email address will not be published.