Home >PHP Framework >ThinkPHP >Using the Container class to implement the ThinkPHP core framework

Using the Container class to implement the ThinkPHP core framework

coldplay.xixi
coldplay.xixiforward
2020-09-07 13:18:533722browse

Using the Container class to implement the ThinkPHP core framework

Related recommendations: thinkphp

ThinkPHP’s## The #Container class provides a static method get(), which can obtain an instance based on the class name or class alias. The created instance will be kept to avoid repeated creation. To implement this method, modify Container.php and add the following code.

//     * ThinkPHP 5 与 6 在此处参数一致//     * @param string $abstract//     * @param array $vars//     * @param bool $newInstance//     */
    public static function get(string $abstract, array $vars = [], bool $newInstance = false)
    {
        return static::getInstance()->make($abstract, $vars, $newInstance);
    }

Write the

getInstance() method, and add a new static attribute $instance to save its own instance.

protected static $instance;public static function getInstance()
    {
        // 创建自身实例
        if (is_null(static::$instance)) {
            static::$instance = new static;
        }
        return static::$instance;
    }

Write the

make() method.

public function make (string $abstract, array $vars = [], bool $newInstance = false)
    {
        // 这里的 $abstract 是包含有命名空间的类名
        if (isset($this->bind[$abstract])) {
            $abstract = $this->bind[$abstract];
        }

        // 如果已经实例化直接返回
        if (isset($this->instances[$abstract]) && !$newInstance) {
            return $this->instances[$abstract];
        }

        // 如果就创建
        $object = $this->invokeClass($abstract, $vars);

        // 保存实例
        if (!$newInstance) {
        $this->instances[$abstract] = $object;
        }

        return $object;
    }

Create an alias array to save attributes

$bind

    protected $bind = [
        'app' => App::class,
        'config' => Config::class,
        'request' => Request::class
    ];

Write

invokeClass() Method

public function invokeClass (string $class, array $vars = [])
    {
        // $vars 为构造函数的参数
        return new $class();
    }

Modify the entry file

index.php

require __DIR__ . '/../core/base.php';use think\Request;$req = \think\Container::get('request');var_dump($req instanceof Request);

The output bool(true) indicates that the

get() method functions normally.

You can also use the magic methods

__get() and __set() to allow external objects to directly operate the container instance.

    public function __get($abstract)
    {
        // 返回容器的类实例
        return $this->make($abstract);
    }public function __set($abstract, $instance)
    {
        if (isset($this->bind[$abstract])) {
            $abstract = $this->bind[$abstract];
        }
        // 装入容器
        $this->instances[$abstract] = $instance;
    }

Test in

index.php

$container = \think\Container::getInstance();// 获取容器中的实例,输出对象var_dump($container->request);// 装入容器$container->contianerName = $container;var_dump($container->contianerName);

Output object(think\Request) indicates success

Want to learn more about programming , please pay attention to the

php training column!

The above is the detailed content of Using the Container class to implement the ThinkPHP core framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete