2017-06-02 70 views
2

我尝试重构我的代码并使用自动加载系统。现在,我在做:自动加载名称空间的类文件

的index.php:

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

我想消除需要线;每当我调用新的Application()或新的Class()时,我都会要求。

我试图象下面这样:

的index.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(); 

Application.php

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

= DEBUG OUTOUT =

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" 

我打算使用这在s中的不同文件中AME项目

更新1: 文件夹结构

App/ 
--Controllers/ 
----XController.php 
----YController.php 
Core/ 
--Controllers/ 
----IBaseController.php 
----BaseController.php 
--Application.php 
index.php 
+0

所有的课程都在同一个基础文件夹中吗?我想你应该把所有的类都放在同一个文件夹中,这样你就可以创建一个'spl_autoload_register()',它将加载到页面顶部(可能在一个包含的配置类型文件中),并覆盖所有的类你的应用?我不认为我会按每班制作自动加载器。这可能会减慢你的应用程序。 – Rasclatt

+0

我所有的课程都不在同一个文件夹中。我编辑显示我的文件夹结构的主题 – reladawrefurhost

回答

0

为了澄清我大约在同一文件夹评论,您的文件夹结构似乎有组织的,我只是确认你不只是上课浮动无处不在。会议或类似的后,我将仅实现一个自动加载磁带机,找两个点(见底例如,如果在多点)

在您的配置文件:

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); 
}); 

如果有更多的地方(或“应用程序”表示伪类库的名字),我会用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和一个Vendor文件夹。

相关问题