dtwkt46424 2015-12-04 16:21
浏览 113

命名空间和spl_autoload_register

I have found several SO questions similar to mine, but am struggling to find an answer that helps me, plus I'd really like to know the best practice for autoloading classes that exist within namespaces.

My folder structure:

root
-- classes
--- Users
---- Users.class.php

And users.php;

<?php
namespace CompanyName\ProjectName\Users;

class UserMapper
{
    // class code here
}

And my autoload function, which sits in the root folder;

/* autoload classes on instatiation */
spl_autoload_register(function($class)
{
    include $_SERVER['DOCUMENT_ROOT'] . '/classes/' . $class . '.class.php';
}); 

And, let's say I call the user class like so;

<?php
    new \CompanyName\ProjectName\User();

Warning: include(/Applications/XAMPP/xamppfiles/htdocs/test_tool/classes/CompanyName\ProjectName\User.class.php): failed to open stream: No such file or directory in...etc

To use spl_autoload_register, do I need to map my folder structure to my namespace structure? I would prefer not to do this as I like to have my classes in the same folder, with sub folders within.

Or do I add extra code to my autoload function?

I have also searched the php manual, and there is no working namespace example, which I find very strange.

Any help would be much appreciated.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanjiu1003 2018-09-13 17:41
    关注

    Disclaimer: I am a beginer at php, my answer may not be correct but i'm confident examining and testing the example bellow will help clarify the use of Namespaces with spl_autoload_register for any beginner like me.

    Consider this folder structure :

    • root/ contains index.php.
    • root/model/ contains A.php & AA.php

    A.php :

    ?php 
    
    namespace company\model;
    
    use \company\model\A;
    
    class A 
    {
        public function speak()
        {
            echo 'hello world! ';
        }
    }

    AA.php :

    ?php 
    
    namespace company\model;
    
    use \company\model\A;
    
    require_once 'A.php';
    
    class AA extends A 
    {
        public function shout()
        {
            echo 'HELLO WOORLD!!!';
        }
    }

    index.php :

    <?php 
        namespace company; 
    
        use \company\model\A;
    
        function classLoader ($className)
        {
            if (file_exists($className.'.php'))
            {
                require_once $className.'.php';
            } else {
                $className = str_replace('\\', '/', $className);
                $className = str_replace('company/', '', $className);
                if (file_exists($className.'.php'))
                    require_once $className.'.php';
                else
                    throw new EXCEPTION('classLoader could not find '.$className.'.php .');
          }
        }
        spl_autoload_register(classLoader);
    
        $obj = new A; 
        //we dont need to write ($obj = new \company\model\A;) 
        //because of statement at line 4
        $obj->speak();
    
        echo '<br/>';
    
        $objA = new \company\model\AA;  
        $objA->shout();
    
        echo '<br/>';
    
        class AB extends \company\model\AA  
        {
            public function doBoth()
            {
                $this->speak();
                $this->shout();
            }
        }
    
        $objB = new AB;
        $objB->doBoth();

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)