Facade Call and Use in TP

What is a Facade, currently considered a class that facilitates the static invocation of the corresponding method to an existing class (which does not define a static method).
The implementation of the specific Facade is as follows:
First look at the core files:
\thinkphp\base.php
`//Register static proxy for core class
Facade::bind([

facade\App::class      => App::class,
facade\Build::class    => Build::class,
facade\Cache::class    => Cache::class,
facade\Config::class   => Config::class,
facade\Cookie::class   => Cookie::class,
facade\Debug::class    => Debug::class,
facade\Env::class      => Env::class,
facade\Hook::class     => Hook::class,
facade\Lang::class     => Lang::class,
facade\Log::class      => Log::class,
facade\Request::class  => Request::class,
facade\Response::class => Response::class,
facade\Route::class    => Route::class,
facade\Session::class  => Session::class,
facade\Url::class      => Url::class,
facade\Validate::class => Validate::class,
facade\View::class     => View::class,]);

You can see how bindings are in containers like this, and find the corresponding binding methods as follows:

` /**

 * Static proxy for bound classes
 * @static
 * @access public
 * @param  string|array  $name    Class Identification
 * @param  string        $class   Class name
 * @return object
 */
public static function bind($name, $class = null)
{
    if (__CLASS__ != static::class) {
        return self::__callStatic('bind', func_get_args());
    }

    if (is_array($name)) {
        self::$bind = array_merge(self::$bind, $name);
    } else {
        self::$bind[$name] = $class;
    }
}`
//You can see that it's also placed in an array, $bind.

Here's how to register the class name in the thinkphpbase.php file below to access the class directly (see here for yourself)
`//Register Class Library Alias
Loader::addClassAlias([

'App'      => facade\App::class,
'Build'    => facade\Build::class,
'Cache'    => facade\Cache::class,
'Config'   => facade\Config::class,
'Cookie'   => facade\Cookie::class,
'Db'       => Db::class,
'Debug'    => facade\Debug::class,
'Env'      => facade\Env::class,
'Facade'   => Facade::class,
'Hook'     => facade\Hook::class,
'Lang'     => facade\Lang::class,
'Log'      => facade\Log::class,
'Request'  => facade\Request::class,
'Response' => facade\Response::class,
'Route'    => facade\Route::class,
'Session'  => facade\Session::class,
'Url'      => facade\Url::class,
'Validate' => facade\Validate::class,
'View'     => facade\View::class,]);

So how do you call static methods, such as

\Cache::set('name','value');

This method, how to call the set method at a specific time, first use the alias mechanism of the above class to find the corresponding facadeCache::class, that is, the class actually called.Then let's look at the content of this class:

namespace think\facade;

use think\Facade;

class Cache extends Facade
{
}

You can see that there is no content, so where does the set method come from, which uses a PHP static method call that fails to invoke, in the Facade class:

 // Call methods of actual classes
    public static function __callStatic($method, $params)
    {
        return call_user_func_array([static::createFacade(), $method], $params);
    }
//When an inaccessible method is called in a static context, u callStatic() is called.    

That is, when the set method cannot be called, the method will be called, which is actually called

call_user_func_array([static::createFacade(), $method], $params)

In fact:

/**
     * Create Facade Instance
     * @static
     * @access protected
     * @param  string    $class          Class name or identity
     * @param  array     $args           variable
     * @param  bool      $newInstance    Whether to create a new instance each time
     * @return object
     */
    protected static function createFacade($class = '', $args = [], $newInstance = false)
    {
        $class       = $class ?: static::class;
        $facadeClass = static::getFacadeClass();
        if ($facadeClass) {
            $class = $facadeClass;
        } elseif (isset(self::$bind[$class])) {
            $class = self::$bind[$class];
        }

        if (static::$alwaysNewInstance) {
            $newInstance = true;
        }
        return Container::getInstance()->make($class, $args, $newInstance);
    }

You can see the last sentence, which is essentially a set method that calls an instance of Cache::class (obtained from the container).

Tags: PHP Session

Posted on Mon, 02 Dec 2019 19:03:14 -0800 by samohtwerdna