dream0776 2013-05-22 00:09
浏览 138
已采纳

PHP:使用命名空间自动加载多个类

I'm trying to build my own framework for internal usage. I got structure like this:

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 contains: (it's currently working only with files located in core directory):

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

My application/controllers/indexcontroller looks like this:

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

?>

And here comes my questions:

[Question 1]

My code is working currently like this:

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

How I can implement including files by classes with namespaces? For example:

$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 

and so on. How I can produce the handling of the namespace?

[Question 2]

Is it a good practice to use single autoload for Classes, Controllers and models or I should define 3 different spl_autoload_register with 3 different methods and why?

  • 写回答

2条回答 默认 最新

  • dss67853 2013-05-22 00:31
    关注

    Question 1:

    In your autoloader, change the \ (for the namespace) into DIRECTORY_SEPARATOR. This should work:

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

    Always use DIRECTORY_SEPARATOR, especially if the software has the potential to be used on other platforms.

    Question 2:

    I would use one and separate your classes by namespace. However, I think that comes down to how you want to structure your framework and how you separate your code. Someone else may be able to better answer that question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 虚心请教几个问题,各位DS,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题