Home  >  Article  >  Backend Development  >  Detailed explanation of PHP namespace and automatic loading instances

Detailed explanation of PHP namespace and automatic loading instances

小云云
小云云Original
2018-03-22 14:09:011698browse

include and require are the two basic methods of introducing files in PHP. There is nothing wrong with using include and require directly in small-scale development, but in large projects, it will cause a lot of include and require accumulation. Such code is inelegant, inefficient, and difficult to maintain.

In order to solve this problem, some frameworks will provide a configuration list of imported files and import the required files when the object is initialized. But this only makes the code more concise, and the introduced effect is still unsatisfactory. After PHP5, with the improvement of PHP's object-oriented support, the __autoload function truly makes automatic loading possible.

* The functions of include and require are the same. The difference is that include will only generate a warning when an error occurs, while require will throw an error and terminate the script.

* The only difference between include_once and include is that include_once will check whether the file has been introduced, and if so, it will not be introduced again.

==================Automatic loading==================

Implementation The simplest way to autoload is to use the __autoload magic method. When the class to be used has not been introduced, this function will be triggered before PHP reports an error, and the undefined class name will be passed in as a parameter. As for the specific logic of the function, this needs to be implemented by the user himself.

First create an autoload.php to do a simple test:

// 类未定义时,系统自动调用function __autoload($class)
{    /* 具体处理逻辑 */
    echo $class;// 简单的输出未定义的类名}new HelloWorld();/**
 * 输出 HelloWorld 与报错信息
 * Fatal error: Class 'HelloWorld' not found */

Through this simple example, we can find that during the instantiation process of the class, the work done by the system is roughly like this ’s:

/* 模拟系统实例化过程 */function instance($class)
{    // 如果类存在则返回其实例
    if (class_exists($class, false)) {        return new $class();
    }    // 查看 autoload 函数是否被用户定义
    if (function_exists('__autoload')) {
        __autoload($class); // 最后一次引入的机会    }    // 再次检查类是否存在
    if (class_exists($class, false)) {        return new $class();
    } else { // 系统:我实在没辙了
        throw new Exception('Class Not Found');
    }
}

After understanding the working principle of the __autoload function, let us use it to implement automatic loading.

First create a class file (it is recommended that the file name be consistent with the class name), the code is as follows:

class [ClassName] 
{    // 对象实例化时输出当前类名
    function __construct()
    {        echo &#39;<h1>&#39; . __CLASS__ . &#39;</h1>&#39;;
    }
}

(I created a HelloWorld class here for demonstration) Next we have to define_ The specific logic of _autoload enables automatic loading:

function __autoload($class)
{    // 根据类名确定文件名
    $file = $class . &#39;.php&#39;;    if (file_exists($file)) {        include $file; // 引入PHP文件    }
}new HelloWorld();/**
 * 输出 <h1>HelloWorld</h1> */

==================Namespace============ ======

In fact, namespaces are not a new thing. Many languages ​​(such as C++) have already supported this feature. It’s just that PHP started relatively late and was not supported until PHP 5.3.

In short, a namespace is an identifier, and its main purpose is to solve the problem of naming conflicts.

Just like in daily life, there are many people with the same name. How to distinguish these people? Then you need to add some additional logos.

It seems good to use your work unit as your logo, so you don’t have to worry about the embarrassment of “name collision”.

Here we will do a small task to introduce Baidu CEO Robin Li:

namespace 百度;class 李彦宏
{    function __construct()
    {        echo &#39;百度创始人&#39;;
    }
}

↑ This is Robin Li’s basic information. Namespace is his unit identification and class is his name.

The namespace is declared through the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code.

new 百度\李彦宏(); // 限定类名new \百度\李彦宏(); // 完全限定类名

↑ Under normal circumstances, whether you are introducing "Baidu Robin Li" or "Baidu Robin Li" to others, they can understand.

When the current namespace is not declared, qualified class names and fully qualified class names are equivalent. Because if you don't specify a space, it defaults to global (\).

namespace 谷歌;new 百度\李彦宏(); // 谷歌\百度\李彦宏(实际结果)new \百度\李彦宏(); // 百度\李彦宏(实际结果)

↑ If you introduce Robin Li to their employees at Google, be sure to specify "Robin Robin of Baidu." Otherwise, he will think that Baidu is a department of Google and Robin Li is just one of its employees.

This example shows the difference between using qualified class names and fully qualified class names in namespaces. (Fully qualified class name = current namespace + qualified class name)

/* 导入命名空间 */use 百度\李彦宏;new 李彦宏(); // 百度\李彦宏(实际结果)/* 设置别名 */use 百度\李彦宏 AS CEO;new CEO(); // 百度\李彦宏(实际结果)/* 任何情况 */new \百度\李彦宏();// 百度\李彦宏(实际结果)

↑ The first situation is that others already know Robin Li, you only need to say the name directly, and he will know who you are referring to. The second situation is that Robin Li is their CEO. If you directly address the CEO, he can react immediately.

Using a namespace only prefixes the class name, which is less likely to cause conflicts. The system will still not automatically import it.

If the file is not introduced, the system will trigger the __autoload function and pass in the qualified class name as a parameter before throwing the "Class Not Found" error.

So the above examples are based on the situation that you have manually introduced the relevant files, otherwise the system will throw "Class 'Baidu\Robin Li' not found".

==================spl_autoload==================

接下来让我们要在含有命名空间的情况下去实现自动加载。这里我们使用 spl_autoload_register() 函数来实现,这需要你的 PHP 版本号大于 5.12。

spl_autoload_register 函数的功能就是把传入的函数(参数可以为回调函数或函数名称形式)注册到 SPL __autoload 函数队列中,并移除系统默认的 __autoload() 函数。

一旦调用 spl_autoload_register() 函数,当调用未定义类时,系统就会按顺序调用注册到 spl_autoload_register() 函数的所有函数,而不是自动调用 __autoload() 函数。

现在,我们来创建一个 Linux 类,它使用 os 作为它的命名空间(建议文件名与类名保持一致):

namespace os; // 命名空间class Linux // 类名{    function __construct()
    {        echo &#39;<h1>&#39; . __CLASS__ . &#39;</h1>&#39;;
    }
}

接着,在同一个目录下新建一个 PHP 文件,使用 spl_autoload_register 以函数回调的方式实现自动加载:

spl_autoload_register(function ($class) { // class = os\Linux
    /* 限定类名路径映射 */
    $class_map = array(        // 限定类名 => 文件路径
        &#39;os\\Linux&#39; => &#39;./Linux.php&#39;,
    );    /* 根据类名确定文件名 */
    $file = $class_map[$class];    /* 引入相关文件 */
    if (file_exists($file)) {        include $file;
    }
});new \os\Linux();

这里我们使用了一个数组去保存类名与文件路径的关系,这样当类名传入时,自动加载器就知道该引入哪个文件去加载这个类了。

但是一旦文件多起来的话,映射数组会变得很长,这样的话维护起来会相当麻烦。如果命名能遵守统一的约定,就可以让自动加载器自动解析判断类文件所在的路径。接下来要介绍的PSR-4 就是一种被广泛采用的约定方式。

=================PSR-4规范==================

PSR-4 是关于由文件路径自动载入对应类的相关规范,规范规定了一个完全限定类名需要具有以下结构:

\8ee79bcbd86d4f3ff58e77260dbf6803(\62dd555a0fb7e11d8719306e139a32e9)*\9f92f7e78eacb2684f956ddec8c620be

如果继续拿上面的例子打比方的话,顶级命名空间相当于公司,子命名空间相当于职位,类名相当于人名。那么李彦宏标准的称呼为 "百度公司 CEO 李彦宏"。

PSR-4 规范中必须要有一个顶级命名空间,它的意义在于表示某一个特殊的目录(文件基目录)。子命名空间代表的是类文件相对于文件基目录的这一段路径(相对路径),类名则与文件名保持一致(注意大小写的区别)。

举个例子:在全限定类名 \app\view\news\Index 中,如果 app 代表 C:\Baidu,那么这个类的路径则是 C:\Baidu\view\news\Index.php

我们就以解析 \app\view\news\Index 为例,编写一个简单的 Demo:

$class = 'app\view\news\Index';/* 顶级命名空间路径映射 */$vendor_map = array(    'app' => 'C:\Baidu',);/* 解析类名为文件路径 */$vendor = substr($class, 0, strpos($class, '\\')); // 取出顶级命名空间[app]$vendor_dir = $vendor_map[$vendor]; // 文件基目录[C:\Baidu]$rel_path = dirname(substr($class, strlen($vendor))); // 相对路径[/view/news]$file_name = basename($class) . '.php'; // 文件名[Index.php]/* 输出文件所在路径 */echo $vendor_dir . $rel_path . DIRECTORY_SEPARATOR . $file_name;

通过这个 Demo 可以看出限定类名转换为路径的过程。那么现在就让我们用规范的面向对象方式去实现自动加载器吧。

首先我们创建一个文件 Index.php,它处于 \app\mvc\view\home 目录中:

namespace app\mvc\view\home;class Index
{    function __construct()
    {        echo &#39;<h1> Welcome To Home </h1>&#39;;
    }
}

接着我们在创建一个加载类(不需要命名空间),它处于 \ 目录中:

class Loader
{    /* 路径映射 */
    public static $vendorMap = array(        &#39;app&#39; => __DIR__ . DIRECTORY_SEPARATOR . &#39;app&#39;,
    );    /**
     * 自动加载器     */
    public static function autoload($class)
    {        $file = self::findFile($class);        if (file_exists($file)) {
            self::includeFile($file);
        }
    }    /**
     * 解析文件路径     */
    private static function findFile($class)
    {        $vendor = substr($class, 0, strpos($class, &#39;\\&#39;)); // 顶级命名空间
        $vendorDir = self::$vendorMap[$vendor]; // 文件基目录
        $filePath = substr($class, strlen($vendor)) . &#39;.php&#39;; // 文件相对路径
        return strtr($vendorDir . $filePath, &#39;\\&#39;, DIRECTORY_SEPARATOR); // 文件标准路径    }    /**
     * 引入文件     */
    private static function includeFile($file)
    {        if (is_file($file)) {            include $file;
        }
    }
}

最后,将 Loader 类中的 autoload 注册到 spl_autoload_register 函数中:

include &#39;Loader.php&#39;; // 引入加载器spl_autoload_register(&#39;Loader::autoload&#39;); // 注册自动加载new \app\mvc\view\home\Index(); // 实例化未引用的类/**
 * 输出: <h1> Welcome To Home </h1> */

示例中的代码其实就是 ThinkPHP 自动加载器源码的精简版,它是 ThinkPHP 5 能实现惰性加载的关键。

至此,自动加载的原理已经全部讲完了,如果有兴趣深入了解的话,可以参考下面的 ThinkPHP 源码。

class Loader
{    protected static $instance = [];    // 类名映射
    protected static $map = [];    // 命名空间别名
    protected static $namespaceAlias = [];    // PSR-4
    private static $prefixLengthsPsr4 = [];    private static $prefixDirsPsr4    = [];    private static $fallbackDirsPsr4  = [];    // PSR-0
    private static $prefixesPsr0     = [];    private static $fallbackDirsPsr0 = [];    // 自动加载的文件
    private static $autoloadFiles = [];    // 自动加载
    public static function autoload($class)
    {        // 检测命名空间别名
        if (!empty(self::$namespaceAlias)) {            $namespace = dirname($class);            if (isset(self::$namespaceAlias[$namespace])) {                $original = self::$namespaceAlias[$namespace] . &#39;\\&#39; . basename($class);                if (class_exists($original)) {                    return class_alias($original, $class, false);
                }
            }
        }        if ($file = self::findFile($class)) {            // Win环境严格区分大小写
            if (IS_WIN && pathinfo($file, PATHINFO_FILENAME) != pathinfo(realpath($file), PATHINFO_FILENAME)) {                return false;
            }

            __include_file($file);            return true;
        }
    }    /**
     * 查找文件
     * @param $class
     * @return bool     */
    private static function findFile($class)
    {        if (!empty(self::$map[$class])) {            // 类库映射
            return self::$map[$class];
        }        // 查找 PSR-4
        $logicalPathPsr4 = strtr($class, &#39;\\&#39;, DS) . EXT;        $first = $class[0];        if (isset(self::$prefixLengthsPsr4[$first])) {            foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) {                if (0 === strpos($class, $prefix)) {                    foreach (self::$prefixDirsPsr4[$prefix] as $dir) {                        if (is_file($file = $dir . DS . substr($logicalPathPsr4, $length))) {                            return $file;
                        }
                    }
                }
            }
        }        // 查找 PSR-4 fallback dirs
        foreach (self::$fallbackDirsPsr4 as $dir) {            if (is_file($file = $dir . DS . $logicalPathPsr4)) {                return $file;
            }
        }        // 查找 PSR-0
        if (false !== $pos = strrpos($class, &#39;\\&#39;)) {            // namespaced class name
            $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)            . strtr(substr($logicalPathPsr4, $pos + 1), &#39;_&#39;, DS);
        } else {            // PEAR-like class name
            $logicalPathPsr0 = strtr($class, &#39;_&#39;, DS) . EXT;
        }        if (isset(self::$prefixesPsr0[$first])) {            foreach (self::$prefixesPsr0[$first] as $prefix => $dirs) {                if (0 === strpos($class, $prefix)) {                    foreach ($dirs as $dir) {                        if (is_file($file = $dir . DS . $logicalPathPsr0)) {                            return $file;
                        }
                    }
                }
            }
        }        // 查找 PSR-0 fallback dirs
        foreach (self::$fallbackDirsPsr0 as $dir) {            if (is_file($file = $dir . DS . $logicalPathPsr0)) {                return $file;
            }
        }        return self::$map[$class] = false;
    }    // 注册classmap
    public static function addClassMap($class, $map = &#39;&#39;)
    {        if (is_array($class)) {
            self::$map = array_merge(self::$map, $class);
        } else {
            self::$map[$class] = $map;
        }
    }    // 注册命名空间
    public static function addNamespace($namespace, $path = &#39;&#39;)
    {        if (is_array($namespace)) {            foreach ($namespace as $prefix => $paths) {
                self::addPsr4($prefix . &#39;\\&#39;, rtrim($paths, DS), true);
            }
        } else {
            self::addPsr4($namespace . &#39;\\&#39;, rtrim($path, DS), true);
        }
    }    // 添加Ps0空间
    private static function addPsr0($prefix, $paths, $prepend = false)
    {        if (!$prefix) {            if ($prepend) {
                self::$fallbackDirsPsr0 = array_merge(
                    (array) $paths,
                    self::$fallbackDirsPsr0
                );
            } else {
                self::$fallbackDirsPsr0 = array_merge(
                    self::$fallbackDirsPsr0,
                    (array) $paths
                );
            }            return;
        }        $first = $prefix[0];        if (!isset(self::$prefixesPsr0[$first][$prefix])) {
            self::$prefixesPsr0[$first][$prefix] = (array) $paths;            return;
        }        if ($prepend) {
            self::$prefixesPsr0[$first][$prefix] = array_merge(
                (array) $paths,
                self::$prefixesPsr0[$first][$prefix]
            );
        } else {
            self::$prefixesPsr0[$first][$prefix] = array_merge(
                self::$prefixesPsr0[$first][$prefix],
                (array) $paths
            );
        }
    }    // 添加Psr4空间
    private static function addPsr4($prefix, $paths, $prepend = false)
    {        if (!$prefix) {            // Register directories for the root namespace.
            if ($prepend) {
                self::$fallbackDirsPsr4 = array_merge(
                    (array) $paths,
                    self::$fallbackDirsPsr4
                );
            } else {
                self::$fallbackDirsPsr4 = array_merge(
                    self::$fallbackDirsPsr4,
                    (array) $paths
                );
            }
        } elseif (!isset(self::$prefixDirsPsr4[$prefix])) {            // Register directories for a new namespace.
            $length = strlen($prefix);            if (&#39;\\&#39; !== $prefix[$length - 1]) {                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
            }
            self::$prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
            self::$prefixDirsPsr4[$prefix]                = (array) $paths;
        } elseif ($prepend) {            // Prepend directories for an already registered namespace.
            self::$prefixDirsPsr4[$prefix] = array_merge(
                (array) $paths,
                self::$prefixDirsPsr4[$prefix]
            );
        } else {            // Append directories for an already registered namespace.
            self::$prefixDirsPsr4[$prefix] = array_merge(
                self::$prefixDirsPsr4[$prefix],
                (array) $paths
            );
        }
    }    // 注册命名空间别名
    public static function addNamespaceAlias($namespace, $original = &#39;&#39;)
    {        if (is_array($namespace)) {
            self::$namespaceAlias = array_merge(self::$namespaceAlias, $namespace);
        } else {
            self::$namespaceAlias[$namespace] = $original;
        }
    }    // 注册自动加载机制
    public static function register($autoload = &#39;&#39;)
    {        // 注册系统自动加载
        spl_autoload_register($autoload ?: &#39;think\\Loader::autoload&#39;, true, true);        // 注册命名空间定义
        self::addNamespace([            &#39;think&#39;    => LIB_PATH . &#39;think&#39; . DS,
            &#39;behavior&#39; => LIB_PATH . &#39;behavior&#39; . DS,
            &#39;traits&#39;   => LIB_PATH . &#39;traits&#39; . DS,
        ]);        // 加载类库映射文件
        if (is_file(RUNTIME_PATH . &#39;classmap&#39; . EXT)) {
            self::addClassMap(__include_file(RUNTIME_PATH . &#39;classmap&#39; . EXT));
        }        // Composer自动加载支持
        if (is_dir(VENDOR_PATH . &#39;composer&#39;)) {
            self::registerComposerLoader();
        }        // 自动加载extend目录
        self::$fallbackDirsPsr4[] = rtrim(EXTEND_PATH, DS);
    }    // 注册composer自动加载
    private static function registerComposerLoader()
    {        if (is_file(VENDOR_PATH . &#39;composer/autoload_namespaces.php&#39;)) {            $map = require VENDOR_PATH . &#39;composer/autoload_namespaces.php&#39;;            foreach ($map as $namespace => $path) {
                self::addPsr0($namespace, $path);
            }
        }        if (is_file(VENDOR_PATH . &#39;composer/autoload_psr4.php&#39;)) {            $map = require VENDOR_PATH . &#39;composer/autoload_psr4.php&#39;;            foreach ($map as $namespace => $path) {
                self::addPsr4($namespace, $path);
            }
        }        if (is_file(VENDOR_PATH . &#39;composer/autoload_classmap.php&#39;)) {            $classMap = require VENDOR_PATH . &#39;composer/autoload_classmap.php&#39;;            if ($classMap) {
                self::addClassMap($classMap);
            }
        }        if (is_file(VENDOR_PATH . &#39;composer/autoload_files.php&#39;)) {            $includeFiles = require VENDOR_PATH . &#39;composer/autoload_files.php&#39;;            foreach ($includeFiles as $fileIdentifier => $file) {                if (empty(self::$autoloadFiles[$fileIdentifier])) {
                    __require_file($file);
                    self::$autoloadFiles[$fileIdentifier] = true;
                }
            }
        }
    }    /**
     * 导入所需的类库 同java的Import 本函数有缓存功能
     * @param string $class   类库命名空间字符串
     * @param string $baseUrl 起始路径
     * @param string $ext     导入的文件扩展名
     * @return boolean     */
    public static function import($class, $baseUrl = &#39;&#39;, $ext = EXT)
    {        static $_file = [];        $key          = $class . $baseUrl;        $class        = str_replace([&#39;.&#39;, &#39;#&#39;], [DS, &#39;.&#39;], $class);        if (isset($_file[$key])) {            return true;
        }        if (empty($baseUrl)) {            list($name, $class) = explode(DS, $class, 2);            if (isset(self::$prefixDirsPsr4[$name . &#39;\\&#39;])) {                // 注册的命名空间
                $baseUrl = self::$prefixDirsPsr4[$name . &#39;\\&#39;];
            } elseif (&#39;@&#39; == $name) {                //加载当前模块应用类库
                $baseUrl = App::$modulePath;
            } elseif (is_dir(EXTEND_PATH . $name)) {                $baseUrl = EXTEND_PATH;
            } else {                // 加载其它模块的类库
                $baseUrl = APP_PATH . $name . DS;
            }
        } elseif (substr($baseUrl, -1) != DS) {            $baseUrl .= DS;
        }        // 如果类存在 则导入类库文件
        if (is_array($baseUrl)) {            foreach ($baseUrl as $path) {                $filename = $path . DS . $class . $ext;                if (is_file($filename)) {                    break;
                }
            }
        } else {            $filename = $baseUrl . $class . $ext;
        }        if (!empty($filename) && is_file($filename)) {            // 开启调试模式Win环境严格区分大小写
            if (IS_WIN && pathinfo($filename, PATHINFO_FILENAME) != pathinfo(realpath($filename), PATHINFO_FILENAME)) {                return false;
            }
            __include_file($filename);            $_file[$key] = true;            return true;
        }        return false;
    }    /**
     * 实例化(分层)模型
     * @param string $name         Model名称
     * @param string $layer        业务层名称
     * @param bool   $appendSuffix 是否添加类名后缀
     * @param string $common       公共模块名
     * @return Object
     * @throws ClassNotFoundException     */
    public static function model($name = &#39;&#39;, $layer = &#39;model&#39;, $appendSuffix = false, $common = &#39;common&#39;)
    {        if (isset(self::$instance[$name . $layer])) {            return self::$instance[$name . $layer];
        }        if (strpos($name, &#39;/&#39;)) {            list($module, $name) = explode(&#39;/&#39;, $name, 2);
        } else {            $module = Request::instance()->module();
        }        $class = self::parseClass($module, $layer, $name, $appendSuffix);        if (class_exists($class)) {            $model = new $class();
        } else {            $class = str_replace(&#39;\\&#39; . $module . &#39;\\&#39;, &#39;\\&#39; . $common . &#39;\\&#39;, $class);            if (class_exists($class)) {                $model = new $class();
            } else {                throw new ClassNotFoundException(&#39;class not exists:&#39; . $class, $class);
            }
        }
        self::$instance[$name . $layer] = $model;        return $model;
    }    /**
     * 实例化(分层)控制器 格式:[模块名/]控制器名
     * @param string $name         资源地址
     * @param string $layer        控制层名称
     * @param bool   $appendSuffix 是否添加类名后缀
     * @param string $empty        空控制器名称
     * @return Object|false
     * @throws ClassNotFoundException     */
    public static function controller($name, $layer = &#39;controller&#39;, $appendSuffix = false, $empty = &#39;&#39;)
    {        if (strpos($name, &#39;/&#39;)) {            list($module, $name) = explode(&#39;/&#39;, $name);
        } else {            $module = Request::instance()->module();
        }        $class = self::parseClass($module, $layer, $name, $appendSuffix);        if (class_exists($class)) {            return new $class(Request::instance());
        } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) {            return new $emptyClass(Request::instance());
        }
    }    /**
     * 实例化验证类 格式:[模块名/]验证器名
     * @param string $name         资源地址
     * @param string $layer        验证层名称
     * @param bool   $appendSuffix 是否添加类名后缀
     * @param string $common       公共模块名
     * @return Object|false
     * @throws ClassNotFoundException     */
    public static function validate($name = &#39;&#39;, $layer = &#39;validate&#39;, $appendSuffix = false, $common = &#39;common&#39;)
    {        $name = $name ?: Config::get(&#39;default_validate&#39;);        if (empty($name)) {            return new Validate;
        }        if (isset(self::$instance[$name . $layer])) {            return self::$instance[$name . $layer];
        }        if (strpos($name, &#39;/&#39;)) {            list($module, $name) = explode(&#39;/&#39;, $name);
        } else {            $module = Request::instance()->module();
        }        $class = self::parseClass($module, $layer, $name, $appendSuffix);        if (class_exists($class)) {            $validate = new $class;
        } else {            $class = str_replace(&#39;\\&#39; . $module . &#39;\\&#39;, &#39;\\&#39; . $common . &#39;\\&#39;, $class);            if (class_exists($class)) {                $validate = new $class;
            } else {                throw new ClassNotFoundException(&#39;class not exists:&#39; . $class, $class);
            }
        }
        self::$instance[$name . $layer] = $validate;        return $validate;
    }    /**
     * 数据库初始化 并取得数据库类实例
     * @param mixed         $config 数据库配置
     * @param bool|string   $name 连接标识 true 强制重新连接
     * @return \think\db\Connection     */
    public static function db($config = [], $name = false)
    {        return Db::connect($config, $name);
    }    /**
     * 远程调用模块的操作方法 参数格式 [模块/控制器/]操作
     * @param string       $url          调用地址
     * @param string|array $vars         调用参数 支持字符串和数组
     * @param string       $layer        要调用的控制层名称
     * @param bool         $appendSuffix 是否添加类名后缀
     * @return mixed     */
    public static function action($url, $vars = [], $layer = &#39;controller&#39;, $appendSuffix = false)
    {        $info   = pathinfo($url);        $action = $info[&#39;basename&#39;];        $module = &#39;.&#39; != $info[&#39;dirname&#39;] ? $info[&#39;dirname&#39;] : Request::instance()->controller();        $class  = self::controller($module, $layer, $appendSuffix);        if ($class) {            if (is_scalar($vars)) {                if (strpos($vars, &#39;=&#39;)) {                    parse_str($vars, $vars);
                } else {                    $vars = [$vars];
                }
            }            return App::invokeMethod([$class, $action . Config::get(&#39;action_suffix&#39;)], $vars);
        }
    }    /**
     * 字符串命名风格转换
     * type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
     * @param string  $name 字符串
     * @param integer $type 转换类型
     * @return string     */
    public static function parseName($name, $type = 0)
    {        if ($type) {            return ucfirst(preg_replace_callback(&#39;/_([a-zA-Z])/&#39;, function ($match) {                return strtoupper($match[1]);
            }, $name));
        } else {            return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
        }
    }    /**
     * 解析应用类的类名
     * @param string $module 模块名
     * @param string $layer  层名 controller model ...
     * @param string $name   类名
     * @param bool   $appendSuffix
     * @return string     */
    public static function parseClass($module, $layer, $name, $appendSuffix = false)
    {        $name  = str_replace([&#39;/&#39;, &#39;.&#39;], &#39;\\&#39;, $name);        $array = explode(&#39;\\&#39;, $name);        $class = self::parseName(array_pop($array), 1) . (App::$suffix || $appendSuffix ? ucfirst($layer) : &#39;&#39;);        $path  = $array ? implode(&#39;\\&#39;, $array) . &#39;\\&#39; : &#39;&#39;;        return App::$namespace . &#39;\\&#39; . ($module ? $module . &#39;\\&#39; : &#39;&#39;) . $layer . &#39;\\&#39; . $path . $class;
    }    /**
     * 初始化类的实例
     * @return void     */
    public static function clearInstance()
    {
        self::$instance = [];
    }
}/**
 * 作用范围隔离
 *
 * @param $file
 * @return mixed */function __include_file($file)
{    return include $file;
}function __require_file($file)
{    return require $file;
}

相关推荐:

PHP命名空间namespace定义详解

完全掌握php命名空间

PHP命名空间详细使用方法

The above is the detailed content of Detailed explanation of PHP namespace and automatic loading instances. 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