gpt4 book ai didi

php - spl_autoload_register 不工作

转载 作者:可可西里 更新时间:2023-10-31 22:12:56 25 4
gpt4 key购买 nike

我在主目录中创建了 5 个文件夹,其中包含 5 个类(Ad_Class、Blocked_Class、Friend_Class、Image_Class、Profile_Class)。我还在提到的文件夹中创建了相应的类,并使用与文件夹相同的名称。即,如果文件夹名称为 Ad_Class,则文件夹中的类也与“类 Ad_Class”中的文件夹名称相同。

在index.php文件中我写了如下代码:

function Ad_Class($name)
{
include "Ad_Class/$name.php";
}
function Blocked_Class($name)
{
include "Blocked_Class/$name.php";
}
function Friend_Class($name)
{
include "Friend_Class/$name.php";
}
function Image_Class($name)
{
include "Image_Class/$name.php";
}
function Profile_Class($name)
{
include "Profile_Class/$name.php";
}

spl_autoload_register("Ad_Class");
spl_autoload_register("Blocked_Class");
spl_autoload_register("Friend_Class");
spl_autoload_register("Image_Class");
spl_autoload_register("Profile_Class");

$a = new Ad_Class;
$b = new Blocked_Class;
$c = new Blocked_Class;
$d = new Image_Class;
$e = new Profile_Class;

执行上述代码后,我收到以下警告:

Warning: include(Ad_Class/Blocked_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4

Warning: include(): Failed opening 'Ad_Class/Blocked_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4

Warning: include(Ad_Class/Image_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4

Warning: include(): Failed opening 'Ad_Class/Image_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4

Warning: include(Blocked_Class/Image_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8

Warning: include(): Failed opening 'Blocked_Class/Image_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8

Warning: include(Friend_Class/Image_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12

Warning: include(): Failed opening 'Friend_Class/Image_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12

Warning: include(Ad_Class/Profile_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4

Warning: include(): Failed opening 'Ad_Class/Profile_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 4

Warning: include(Blocked_Class/Profile_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8

Warning: include(): Failed opening 'Blocked_Class/Profile_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 8

Warning: include(Friend_Class/Profile_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12

Warning: include(): Failed opening 'Friend_Class/Profile_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 12

Warning: include(Image_Class/Profile_Class.php): failed to open stream: No such file or directory in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 16

Warning: include(): Failed opening 'Image_Class/Profile_Class.php' for inclusion (include_path='.;C:\Users\Robert\Documents\web development\xampp\php\PEAR') in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\development.php on line 16

为什么会这样?谁能解释一下?

更新:我发现以下代码有效。如果您有任何建议,请随时分享!

function ad_class($class)
{
if(!class_exists($class) && $class == "Ad_Class")
{
include "Ad_Class/$class" . ".php";
}
elseif(!class_exists($class))
{
return;
}
}

function blocked_class($class)
{
if(!class_exists($class) && $class == "Blocked_Class")
{
include "Blocked_Class/$class" . ".php";
}
elseif(!class_exists($class))
{
return;
}
}

function friend_class($class)
{
if(!class_exists($class) && $class == "Friend_Class")
{
include "Friend_Class/$class" . ".php";
}
elseif(!class_exists($class))
{
return;
}
}

function image_class($class)
{
if(!class_exists($class) && $class == "Image_Class")
{
include "Image_Class/$class" . ".php";
}
elseif(!class_exists($class))
{
return;
}
}

function profile_class($class)
{
if(!class_exists($class) && $class == "Profile_Class")
{
include "Profile_Class/$class" . ".php";
}
elseif(!class_exists($class))
{
return;
}
}
spl_autoload_register("ad_class");
spl_autoload_register("blocked_class");
spl_autoload_register("friend_class");
spl_autoload_register("image_class");
spl_autoload_register("profile_class");

$a = new Ad_Class;
$a->ad();

$b = new Blocked_Class;
$b->block();

$c = new Friend_Class;
$c->fr();

$d = new Image_Class;
$d->image();

它产生以下内容:广告封锁 friend 图片

无非是显示实例化对象的类名,以查看代码是否在加载类时正确工作。

最佳答案

spl_autoload_register() 堆栈,因此它将运行您为您尝试创建的每个类定义的所有五个函数,无论它是哪个类。以这个例子为例,并根据您的需要对其进行修改:

spl_autoload_register( function($name) {
if (is_file('/classes/'.$name.'.class.php')) {
require_once('/classes/'.$name.'.class.php');
}
});

使用 is_file() 检查文件将避免所有那些No such file or directory 警告的麻烦。根据您的文件路径布局,您可能需要扩展我用 switch() 语句或类似语句给出的匿名函数示例。

关于php - spl_autoload_register 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14776507/

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