Does PHP namespace autoloading have to use folders?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 13:57:40

问题


I am quite confused in implementing namespace in php, especially when it comes to alias - importing classes.

I have followed the tutorial from this tutorial:

  • Leveraging PHP V5.3 namespaces for readable and maintainable code (by Don Denoncourt; 1 Mar 2011; for IBM Developerworks)

But I don't understand - when __autoload is used, why I have to store the alias classes in folders, but when __autoload is not used, the alias in namespace are just fine, like this below,

<?php
namespace barbarian;
class Conan {
    var $bodyBuild = "extremely muscular";
    var $birthDate = 'before history';
    var $skill = 'fighting';
}
namespace obrien;
class Conan {
    var $bodyBuild = "very skinny";
    var $birthDate = '1963';
    var $skill = 'comedy';
}
use \barbarian\Conan as mother;
$conan = new mother();
var_dump($conan);
var_dump($conan->bodyBuild);

$conan = new \obrien\Conan();
var_dump($conan);
var_dump($conan->birthDate);
?>

While this I will get error, if I don't store Conan.php in the folder of barbarian

<?php
require_once "autoload.php"; 
use \barbarian\Conan as Cimmerian;
$conan = new Cimmerian();
var_dump($conan);
?>

the error message,

Warning: require(barbarian/Conan.php): failed to open stream: No such file or directory in C:\wamp\www\test\2013\php\namepsace\autoload.php on line 12

The autoload.php:

<?php
function __autoload($classname) {
  $classname = ltrim($classname, '\\');
  $filename  = '';
  $namespace = '';
  if ($lastnspos = strripos($classname, '\\')) {
    $namespace = substr($classname, 0, $lastnspos);
    $classname = substr($classname, $lastnspos + 1);
    $filename  = str_replace('\\', '/', $namespace) . '/';
  }
  $filename .= str_replace('_', '/', $classname) . '.php';
  require $filename;
}
?>

Is it a must to store alias classes in folders? Is it possible to import the classes without storing them in folders when autoload is used?


回答1:


Autoloading classes with namespaces means it has to follow a convention, usually that convention involves using folders (compare with PSR-0).

If you have classes that sometimes follow that convention, then how would the autoloader know when to use folders or not?

So ultimately, yes classes should be stored in folders according to their namespaces. If you think the folder structure does not make sense, then you should change both namespaces and folder structure to reflect what you really want.




回答2:


You don't have to use directories if you don't wish to - that example does, of course, but you can implement loading any way you wish. You could put all classes in a single folder, and use underscore characters in the form Classes/Barbarian_Conan.php. However that could get quite messy, and you'd have to work out what to do if you encountered an underscore in a class or namespace name!

That all said, it is a good idea to use directories this way. For maximum interoperability with other PHP software, furthermore, it is a good idea to follow PSR-0. More details about this standards group here.

As I discovered from the web, and you discovered from experimentation, neither the class name nor the namespace path names may contain a PHP reserved word.



来源:https://stackoverflow.com/questions/15688805/does-php-namespace-autoloading-have-to-use-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!