简体   繁体   English

PHP名称空间自动加载

[英]PHP namespace autoload

Trying to understand how namespaces and autoload works on PHP 试图了解名称空间和自动加载如何在PHP上工作

Server.php located at core/server.php Server.php位于core / server.php

namespace core\server

{
     class Main
     {
        public function getTopic()
        {
            $get_params = $_GET;    
            if (empty($get_params)) $get_params = ['subtopic' => 'test'];   
            return $get_params;
        }
     }
}

and Index.php 和Index.php

spl_autoload_register();

use core\server as subtopic;

$test = new subtopic\Main();

var_dump($test);

It cant load the class core/server/Main 它无法加载类core / server / Main

Autoload doesn't work that way. 自动加载功能无法正常工作。 First I will explain how autoloaders works. 首先,我将解释自动加载器的工作原理。

spl_autoload_register() is a function to register a function you have in your code to server as an autoloader, the standard function would be: spl_autoload_register()是用于将代码中具有的功能注册为自动加载器到服务器的功能,标准功能为:

define('APP_PATH','/path/to/your/dir');

function auto_load($class)
{
    if(file_exists(APP_PATH.$class.'.php'))
    {
        include_once APP_PATH.$class.'.php';
    }
}

spl_autoload_register('auto_load');

The constant APP_PATH would be the path to your directory where your code lives. 常量APP_PATH将是代码所在目录的路径。 As you noticed it the param that is passed to spl_autoload_register is the name of my function, this register the function so when a class is instanciated it runs that function. 正如您注意到的那样,传递给spl_autoload_register的参数是我的函数的名称,此函数注册该函数,因此当实例化一个类时,它将运行该函数。

Now an effective way to use autoloaders and namespaces would be the following: 现在,使用自动加载器和名称空间的有效方法如下:

file - /autoloader.php 文件-/ autoloader.php

define('APP_PATH','/path/to/your/dir');
define('DS', DIRECTORY_SEPARATOR);

function auto_load($class)
{
    $class = str_replace('\\', DS, $class);
    if(file_exists(APP_PATH.$class.'.php'))
    {
        include_once APP_PATH.$class.'.php';
    }
}

spl_autoload_register('auto_load');

file - /index.php 文件-/ index.php

include 'autoloader.php';

$tree = new Libs\Tree();

$apple_tree = new Libs\Tree\AppleTree();

file - /Libs/Tree.php 文件-/ Libs/Tree.php

namespace Libs;
class Tree
{
   public function __construct()
   {
      echo 'Builded '.__CLASS__;
   } 
}

file - /Libs/Tree/AppleTree.php 文件-/ Libs/Tree/AppleTree.php

namespace Libs\Tree;
class AppleTree
{
    public function __construct()
    {
       echo 'Builded '.__CLASS__;
    } 
 }

I'm using namespaces and autoload to load my functions in a nicely way, you can use namespace to describe in what dir your class resides and uses the autoloader magic to load it without any problems. 我正在使用名称空间和自动加载以一种很好的方式加载我的函数,您可以使用名称空间来描述您的类驻留在哪个目录中,并使用自动加载器魔术来加载它而没有任何问题。

Note: I used the constant 'DS', because in *nix it uses '/' and in Windows it uses '\\', with DIRECTORY_SEPARATOR we don't have to worry where the code is going to run, because it will be "path-compatible" 注意:我使用了常数'DS',因为在* nix中使用'/',而在Windows中使用'\\',通过DIRECTORY_SEPARATOR,我们不必担心代码将在哪里运行,因为它将是“路径兼容”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM