【发布时间】:2014-09-21 10:27:00
【问题描述】:
我正在尝试了解如何为带有命名空间的普通应用程序定义一个有效的自动加载器。
我创建了以下结构:
public
index.php
src
Models
WordGenerator.php
显然,以后它会增长。 WordGenerator.php 文件如下:
<?php
namespace Models;
class WordGenerator {
public $filename;
public function __construct($filename) {
$this->_filename = $filename;
}
}
现在我尝试在index.php 中创建这个对象的实例:
<?php
use \Models;
$wg = new WordGenerator("words.english");
显然我得到了致命错误,因为我没有定义autoloader。但是,根据文档,我只能通过$classname。那么我应该如何定义自动加载器函数来获取use声明的命名空间呢??
[编辑]
刚才我理解错了。所以这是我的index.php 代码:
spl_autoload_register(function ($className) {
$className = ltrim($className, '\\');
$prepend = "..\\src\\";
$fileName = "{$prepend}";
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = $prepend.str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
});
//require '../src/Models/WordGenerator.php';
use Models;
$wg = new WordGenerator("words.english");
echo $wg->getRandomWord();
现在,这不起作用,我得到:
Warning: The use statement with non-compound name 'Models' has no effect in C:\xampp\htdocs\Hangman\public\index.php on line 22
Warning: require(..\src\WordGenerator.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Hangman\public\index.php on line 16
Fatal error: require(): Failed opening required '..\src\WordGenerator.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\Hangman\public\index.php on line 16
但是,当我将 new WordGenerator 更改为 new \Models\WordGenerator 时,它可以工作。现在的问题是:如何将 use 语句声明的命名空间传递给 autoloader 函数以使其正常工作?
【问题讨论】:
标签: php namespaces php-5.3 spl-autoload-register