简体   繁体   English

命名空间和spl_autoload_register

[英]Namespaces and 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. 我发现了几个类似于我的SO问题,但是我一直在努力寻找一个对我有帮助的答案,而且我真的很想知道自动加载名称空间中存在的类的最佳实践。

My folder structure: 我的文件夹结构:

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

And users.php; 和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 警告:include(/Applications/XAMPP/xamppfiles/htdocs/test_tool/classes/CompanyName\\ProjectName\\User.class.php):无法打开流:...等中没有此类文件或目录

To use spl_autoload_register, do I need to map my folder structure to my namespace structure? 要使用spl_autoload_register,是否需要将文件夹结构映射到命名空间结构? 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. 我还搜索了php手册,没有可用的名称空间示例,我觉得很奇怪。

Any help would be much appreciated. 任何帮助将非常感激。

Thanks in advance. 提前致谢。

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. 免责声明:我是php的初学者,我的回答可能不正确,但是我有信心检查和测试以下示例示例,这将有助于为像我这样的任何初学者阐明使用带有spl_autoload_register命名空间

Consider this folder structure : 考虑以下文件夹结构:

  • root/ contains index.php. root / 包含 index.php。
  • root/model/ contains A.php & AA.php root / model / 包含 A.php和AA.php

A.php : A.php:

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

AA.php : 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 : 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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM