Home > PHP Framework > ThinkPHP > body text

Using the Container class to implement the ThinkPHP core framework

coldplay.xixi
Release: 2020-09-07 13:18:53
forward
3694 people have browsed it

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);
    }
Copy after login

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;
    }
Copy after login

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;
    }
Copy after login

Create an alias array to save attributes

$bind

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

Write

invokeClass() Method

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

Modify the entry file

index.php

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

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;
    }
Copy after login

Test in

index.php

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

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!

Related labels:
source:learnku.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template