如果我正确地得到了SPL Autoloader的行为,它就在一个“全局范围”中工作,对吗?如果我们有这个密码:
class Autoloader {
protected static $instance = NULL;
public static function get_instance() {
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
public function init() {
spl_autoload_register(array($this,'autoload'));
}
private function autoload() {
// to the autoload magic
}
}
$autoloader = new Autoloader();
$autoloader->init();
// or
add_action('muplugins_loaded', array(Autoloader::get_instance(),'init'));..。它适用于应用程序的其余部分,或者如果连接到Wordpress动作,从钩子开始,对吗?
在我看来,这似乎不太方便,特别是当您在更大的框架(如Wordpress)中工作时。是否有办法将SPL Autoload的范围限制在特定的上下文中:
return false,它们的名称不太像某种模式)?我知道,为了避免冲突和错误,我可以在autoload()函数中添加一些条件语句,这看起来不太有效。
谢谢!是的,很有可能我只是忽略了一些东西。
编辑
通过名称空间模拟目录结构:它实际上在Wordpress中是不可能的,是吗?如果您想要的是某种通信和逻辑结构,比方说,必须使用插件和themes。(@RoyalBg)
Not查找文件:如何确定什么时候不查找文件是“可以”的,什么时候不是?根据功能的逻辑?在我看来,缩小自动装配机的范围将是更优雅的解决方案。(@乔恩,马克·贝克)
但如果我很好地理解你的评论,SPL Autoloader在默认情况下确实适用于“全球”。
发布于 2014-05-05 10:34:55
您可以注册多个自动加载器函数/方法。SPL将遍历它们,直到加载所需的类为止。
<?php
function loaderA($name) { // Do Stuff }
function loaderB($name) { // Do Other Stuff }
spl_autoload_register('functionA');
spl_autoload_register('functionB');
?>SPL将一个接一个地完成这些注册功能。首先是functionA,然后是functionB,如果请求的类不是通过functionA加载的。
编辑:
final class LoaderException extends \Exception{}
class Loader {
/**
* Project-Root-Directory
* @var string
*/
protected static $AbsPath;
/**
* Directory-List
* @var array
*/
protected static $DirList;
/**
* DO NOT INSTANTIATE
*/
private function __construct() {}
/**
* The actual autoloader.
* @param string $name Class-Name
* @return void
* @throws LoaderException
*/
public static function load($name) {
if(!is_string($name))
throw new \InvalidArgumentException('Argument is not a string.');
$a = isset(static::$AbsPath) ? static::$AbsPath : '';
$f = str_replace('\\', '/', $name).'.php';
if(!is_array(static::$DirList)) {
if(file_exists($a.$f))
return include $a.$f;
if(file_exists(getcwd().$f))
return include getcwd().$f;
throw new LoaderException('Unable to load "'.$name.'".');
}
foreach(static::$DirList as $d) {
if(file_exists($a.$d.$f))
return include $a.$d.$f;
}
throw new LoaderException('Unable to load "'.$name.'".');
}
/**
* Registers the Loader.
*/
public static function register() {
spl_autoload_register(__CLASS__.'::load');
}
/**
* Unregisters the Loader.
*/
public static function unregister() {
spl_autoload_unregister(__CLASS__.'::load');
}
/**
* Adds one, or more, directories to the Directory-List.
* @throws LoaderException
*/
public static function addDir() {
foreach(func_get_args() as $k => $v) {
if(!is_string($v))
throw new \InvalidArgumentException('Argument #'.($k+1).' is not a string.');
if(!is_dir($v))
throw new \InvalidArgumentException('Argument #'.($k+1).' is not a directory.');
if(!is_array(static::$DirList) or !in_array($v, static::$DirList))
static::$DirList[] = $v;
}
}
/**
* Removes one, or more, directories from the Directory-List.
* @throws LoaderException
*/
public static function removeDir() {
foreach(func_get_args() as $k => $v) {
if(!is_string($v))
throw new \InvalidArgumentException('Argument #'.($k+1).' is not a string.');
if(in_array($v, static::$DirList))
unset(static::$DirList[array_search($v, static::$DirList)]);
}
}
/**
* Sets the Absolute-Path for the loader, this will speed up the loading process.
* @param string $path Absolute-Path to the Project root directory
* @throws LoaderException
*/
public static function setAbsPath($path = null) {
if(isset($path)) {
if(!is_string($path))
throw new \InvalidArgumentException('Argument is not a string.');
if(!is_dir($path))
throw new \InvalidArgumentException('Invalid path "'.$path.'".');
}
static::$AbsPath = $path;
}
}这是我经常使用的基本自动加载程序,它将名称空间识别为子目录,并可以在其中搜索类的特定目录。
https://stackoverflow.com/questions/23470058
复制相似问题