Home  >  Article  >  Backend Development  >  About Dependency Injection in PHP

About Dependency Injection in PHP

不言
不言Original
2018-06-22 11:21:331713browse

This article mainly introduces PHP dependency inversion (Dependency Injection) code examples. This article only provides implementation code. Friends in need can refer to

Implementation class:

<?php
 
class Container
{
    protected $setings = array();
 
    public function set($abstract, $concrete = null)
    {
        if ($concrete === null) {
            $concrete = $abstract;
        }
 
        $this->setings[$abstract] = $concrete;
    }
 
    public function get($abstract, $parameters = array())
    {
        if (!isset($this->setings[$abstract])) {
            return null;
        }
 
        return $this->build($this->setings[$abstract], $parameters);
    }
 
    public function build($concrete, $parameters)
    {
        if ($concrete instanceof Closure) {
            return $concrete($this, $parameters);
        }
 
        $reflector = new ReflectionClass($concrete);
 
        if (!$reflector->isInstantiable()) {
            throw new Exception("Class {$concrete} is not instantiable");
        }
 
        $constructor = $reflector->getConstructor();
 
        if (is_null($constructor)) {
            return $reflector->newInstance();
        }
 
        $parameters = $constructor->getParameters();
        $dependencies = $this->getDependencies($parameters);
 
        return $reflector->newInstanceArgs($dependencies);
    }
 
    public function getDependencies($parameters)
    {
        $dependencies = array();
        foreach ($parameters as $parameter) {
            $dependency = $parameter->getClass();
            if ($dependency === null) {
                if ($parameter->isDefaultValueAvailable()) {
                    $dependencies[] = $parameter->getDefaultValue();
                } else {
                    throw new Exception("Can not be resolve class dependency {$parameter->name}");
                }
            } else {
                $dependencies[] = $this->get($dependency->name);
            }
        }
 
        return $dependencies;
    }
}

Implementation example:

<?php
 
require &#39;container.php&#39;;
 
 
interface MyInterface{}
class Foo implements MyInterface{}
class Bar implements MyInterface{}
class Baz
{
    public function __construct(MyInterface $foo)
    {
        $this->foo = $foo;
    }
}
 
$container = new Container();
$container->set(&#39;Baz&#39;, &#39;Baz&#39;);
$container->set(&#39;MyInterface&#39;, &#39;Foo&#39;);
$baz = $container->get(&#39;Baz&#39;);
print_r($baz);
$container->set(&#39;MyInterface&#39;, &#39;Bar&#39;);
$baz = $container->get(&#39;Baz&#39;);
print_r($baz);

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the development of PHP and jQuery registration module

About PHP – Introduction to EasyUI DataGrid data storage method

The above is the detailed content of About Dependency Injection in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn