2013-05-22 117 views
2

我正嘗試構建自己的內部使用框架。 我得到的結構是這樣的:PHP:使用命名空間自動加載多個類

index.php 
boot/
    booter.php 
application/
    controllers/
      indexcontroller.php 
core/
    template.class.php 
    model.class.php 
    controller.class.php 
    cache/
     memcached.php 
    something/
     something.php 

Booter.php包含:(目前只有與位於核心目錄中的文件工作):

class Booter 
{ 
private static $controller_path, $model_path, $class_path; 

public static function setDirs($controller_path = 'application/controllers', $model_path = 'application/models', $classes_path = 'core') 
{ 
    self::$controller_path = $controller_path; 
    self::$model_path  = $model_path; 
    self::$class_path  = $classes_path; 

    spl_autoload_register(array('Booter', 'LoadClass')); 
    if (DEBUG) 
     Debugger::log('Setting dirs...'); 
} 

protected static function LoadClass($className) 
{ 
    $className = strtolower($className); 

    if (file_exists(DIR . '/' . self::$model_path . '/' . $className . '.php')) 
    { 
     require(DIR . '/' . self::$model_path . '/' . $className . '.php'); 
    }  
    else if (file_exists(DIR . '/' . self::$class_path . '/' . $className . '.class.php')) 
    { 
     require(DIR . '/' . self::$class_path . '/' . $className . '.class.php'); 
    } 
    else if (file_exists(DIR . '/' . self::$controller_path . '/' . $className . '.php')) 
    { 
     require(DIR . '/' . self::$controller_path . '/' . $className . '.php'); 
    } 

    if (DEBUG) 
     Debugger::log('AutoLoading classname: '.$className); 
} 
} 

我的應用程序/控制器/索引控制器看起來是這樣的:

<? 
class IndexController extends Controller 
{ 
    public function ActionIndex() 
    { 
      $a = new Model; // It works 
      $a = new Controller; //It works too 
    } 
} 

?> 

這裏來我的問題:

[問題1]

我的代碼工作目前是這樣的:

$a = new Model; // Class Model gets included from core/model.class.php 

我怎樣才能實現,包括通過使用命名空間的類文件?例如:

$a = new Cache\Memcached; // I would like to include file from /core/CACHE/Memcached.php 
$a = new AnotherNS\smth; // i would like to include file from /core/AnotherNS/smth.php 

等等。我怎樣才能產生處理命名空間?

[問題2]

的是它使用的類,控制器和模型或者我應該定義具有3個不同的方法,爲什麼3個不同的spl_autoload_register單自動加載一個好的做法呢?

回答

2

問題1:

在磁帶自動加載機,改變\(對於命名空間)爲DIRECTORY_SEPARATOR。這應該工作:

protected static function LoadClass($className) 
{ 
    $className = strtolower($className); 
    $className = str_replace('\\', DIRECTORY_SEPARATOR, $className); 
... 
} 

始終使用DIRECTORY_SEPARATOR,特別是如果該軟件已被其他平臺上使用的潛力。

問題2:

我會用一個和命名空間的類分開。但是,我認爲這取決於您想如何構建框架以及如何區分代碼。其他人可能能夠更好地回答這個問題。

2

我通常在應用程序根目錄下的文件夾conf中有一個bootstrap.php文件。我的代碼通常位於一個src文件夾中,也設在根裏面,所以,這個工作正常,我:

<?php 

define('APP_ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR); 

set_include_path(
    implode(PATH_SEPARATOR, 
     array_unique(
      array_merge(
       array(
        APP_ROOT . 'src', 
        APP_ROOT . 'test' 
       ), 
       explode(PATH_SEPARATOR, get_include_path()) 
      ) 
     ) 
    ) 
); 

spl_autoload_register(function ($class) { 
    $file = sprintf("%s.php", str_replace('\\', DIRECTORY_SEPARATOR, $class)); 
    if (($classPath = stream_resolve_include_path($file)) != false) { 
     require $classPath; 
    } 
}, true); 

可以概括這個到你的「足球運動員」級​​和追加目錄包含路徑。如果你有明確定義的名稱空間名稱,就不會出現問題。

編輯: 如果你按照PSR-1這個工程。

相關問題