dream0776 2013-05-21 16: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-21 16: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 潜在扩散模型的Unet特征提取
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部