具有命名空间的Autoload类文件

时间:2017-06-02 12:25:27

标签: php class spl-autoload-register

我尝试重构我的代码并使用自动加载系统。现在,我正在做:

的index.php:

{systemnotes.date}

我想消除require行;每当我调用new Application()或new Class();

时,我都会要求

我尝试如下:

的index.php

namespace example_com;
use example_com\Core\Application;
require 'Application.php';
$app = new Application();
$app->MyCustomFunction();

Application.php

namespace example_com;
use example_com\Core\Application;

spl_autoload_register(function($className) {
    $namespace = str_replace("\\", "/", __NAMESPACE__);
    $className = str_replace("\\", "/", $className);
    $class = ROOT . (empty($namespace) ? "" : $namespace . "/") . "{$className}.php";
    include_once($class);
});

//require 'Application.php';//remove this
$app = new Application();
$app->MyCustomFunction();

= DEBUG OUTOUT =

namespace example_com\Core;
class Application
{
    //..contruct, properties, functions
}

我打算在同一个项目的不同文件中使用它

更新1: 文件夹结构

spl_autoload_register(function($className) { //$className: "example_com/Core/Application"
    $namespace = str_replace("\\", "/", __NAMESPACE__); //$namespace: "example_com"
    $className = str_replace("\\", "/", $className);
    $class = ROOT . (empty($namespace) ? "" : $namespace . "/") . "{$className}.php"; //$className: "example_com/Core/Application" $namespace: "example_com" $class: "example_com/example_com/Core/Application.php"

1 个答案:

答案 0 :(得分:1)

为了澄清我在同一个文件夹中的评论,你的文件夹结构似乎有条理,我只是确认你不只是在各处都有浮动的课程。我会在会话或类似之后实现一个自动装载器,查看两个位置(如果在更多位置,请参见底部示例)

在您的配置文件中:

define('DS',DIRECTORY_SEPARATOR);

spl_autoload_register(function($class) {
    # Set base path
    $default = ROOT.DS.'Core';
    # Set client/vendor path (provided "App" is the
    # name of the root containing vendor libraries)
    $app     = ROOT.DS.'App';
    # Create path
    $path    = str_replace('\\',DS,$class).'.php';
    # Check first that class exists in core
    if(is_file($inc = str_replace(DS.DS,DS,$default.DS.$path)))
        require_once($inc);
    # Check if the class is in the "App" folder
    elseif(is_file($inc = str_replace(DS.DS,DS,$app.DS.$path)))
        require_once($inc);
});

如果有更多地方(或“App”代表伪类库名称),我会使用json文件和目录路径:

<强> /Core/prefs/namespaces.json

["NewFolder/Classes","OtherFolder/Somewhere/Deeper/Classes","Vendor/Classes"]

在您的配置文件中:

define('DS',DIRECTORY_SEPARATOR);

spl_autoload_register(function($class) {
    $default = ROOT.DS.'Core';
    $app     = ROOT.DS.'App';
    $path    = str_replace('\\',DS,$class).'.php';

    if(is_file($inc = str_replace(DS.DS,DS,$default.DS.$path)))
        require_once($inc);
    elseif(is_file($inc = str_replace(DS.DS,DS,$app.DS.$path)))
        require_once($inc);
    # Here is where you will fetch the array of namespaces
    else {
        # Fetch and decode
        $namespaces = json_decode(file_get_contents(ROOT.DS.'Core'.DS.'prefs'.DS.'namespaces.json'),true);
        # Check to make sure there are namespaces to loop over
        if(is_array($namespaces) && !empty($namespaces)) {
            # Loop
            foreach($namespaces as $base) {
                # Check if the class file exists and include if it does
                if(is_file($inc = str_replace(DS.DS,DS,ROOT.DS.$base.DS.$path)))
                    require_once($inc);
            }
        }
    }
});

在上面的例子中,我可能会转向一个或两个托管所有供应商库(包括您的Core库)或两个可以搜索的文件夹,以便Core和{{} {1}}文件夹。