PHP名称空间自动加载是否必须使用文件夹? [英] Does PHP namespace autoloading have to use folders?

查看:80
本文介绍了PHP名称空间自动加载是否必须使用文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在php中实现名称空间感到非常困惑,尤其是在涉及别名-导入类时.

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)

但是我不明白-使用__autoload时,为什么我必须将别名类存储在文件夹中,但是使用__autoload ,命名空间中的别名就很好,如下所示,

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

这是错误的,如果我不将Conan.php存储在barbarian文件夹中

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

错误消息,

警告:require(barbarian/Conan.php):无法打开流:否这样 C:\ wamp \ www \ test \ 2013 \ php \ namepsace \ autoload.php中的文件或目录 在第12行

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

autoload.php:

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

将别名类存储在文件夹中是否是必须?使用autoload时是否可以导入类而不将其存储在文件夹中?

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?

推荐答案

使用名称空间自动加载类意味着必须遵循约定,通常该约定涉及使用文件夹(

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.

这篇关于PHP名称空间自动加载是否必须使用文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆