gpt4 book ai didi

PHP5 AutoLoader SPL_AutoLoad 失败

转载 作者:可可西里 更新时间:2023-10-31 23:34:10 27 4
gpt4 key购买 nike

我将下面的代码作为一个自动加载类,但是看起来 clean 方法根本不起作用,它总是退回到 dirty 方法。

我是否错误地使用了 spl_autoload?如果是这样,正确(更好)的方法是什么?这是低效的吗,如何改进?

尽管使用此方法时,我总是得到诸如 bottom 之类的输出,在某些情况下,它只是找不到类,但不会抛出任何错误 我将显示错误设置为 1并检查了错误日志,但完全丢失了。

代码初始化为

require "vendor/AutoLoader.class.php";
self::setGlobal("autoloader", AutoLoader::init());

类如下:

public static $instance;
private $_src=array('vendor/', 'lib/', '');
private $_sub=array('base/', '');
private $_ext=array('.php', 'class.php', 'lib.php');

/* initialize the autoloader class */
public static function init(){
if(self::$instance==NULL){
self::$instance=new self();
}
return self::$instance;
}

/* put the custom functions in the autoload register when the class is initialized */
private function __construct(){
spl_autoload_register(array($this, 'clean'));
spl_autoload_register(array($this, 'dirty'));
}

/* the clean method to autoload the class without any includes, works in most cases */
private function clean($class){
$class=str_replace('_', '/', $class);
spl_autoload_extensions(implode(',', $this->_ext));
foreach($this->_src as $resource){
foreach($this->_sub as $sub){
echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
set_include_path(pegFramework::getGlobal("baseDir") . $resource.$sub);
spl_autoload($class);
if(class_exists($class)) {
echo 'Found and clean included '.$class.' in '.$resource.$sub."<br />";
break 2;
}
}
}
}

/* the dirty method to autoload the class after including the php file containing the class */
private function dirty($class){
global $docroot;
$class=str_replace('_', '/', $class);
foreach($this->_src as $resource){
foreach($this->_ext as $ext){
foreach($this->_sub as $sub){
echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
if(@include(pegFramework::getGlobal("baseDir") . $resource . $sub. $class . $ext)) {
echo 'Found and dirty included '.$class.' as '.$resource . $sub. $class . $ext."<br />";
break 3;
}
}
}
}
spl_autoload($class);
}

Trying to load pegDatabase via pegAutoloader::clean()
...snip...
Trying to load pegDatabase via pegAutoloader::clean()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load basepegDatabase via pegAutoloader::clean()
...snip...
Trying to load basepegDatabase via pegAutoloader::clean()
Trying to load basepegDatabase via pegAutoloader::dirty()
Found and dirty included basepegDatabase as vendor/base/basepegDatabase.php
Found and dirty included pegDatabase as vendor/pegDatabase.php
Trying to load pegRequest via pegAutoloader::clean()
...snip...
Trying to load pegRequest via pegAutoloader::clean()
Trying to load pegRequest via pegAutoloader::dirty()
Trying to load pegRequest via pegAutoloader::dirty()
Found and dirty included pegRequest as vendor/pegRequest.php
Trying to load pegFacebook via pegAutoloader::clean()
...snip...
Trying to load pegFacebook via pegAutoloader::clean()
Trying to load pegFacebook via pegAutoloader::dirty()
...snip...
Trying to load pegFacebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::clean()
Trying to load Facebook via pegAutoloader::clean()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()

最佳答案

我认为是对 spl_autoload 的调用那是在绊倒你。在php documentation的评论中, 我找到了 this :

Note this function will LOWERCASE the class names its looking for, dont be confused when it cant find Foo_Bar.php

因此,调用 spl_autoloadpegFacebookFacebook将触发搜索名为 pegfacebook.php 的文件或 facebook.class.php .

不过,如果我是你,我会看看 Symfony 的 UniversalClassLoader有关更简化的类加载处理的示例。您可能会调整它以使用特定的扩展,但我至少会考虑寻找一种新方法来处理这个问题。

关于PHP5 AutoLoader SPL_AutoLoad 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7285098/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com