为什么我的自动加载功能找不到特定类
这是我的文件夹结构:
-classes
-controller
-ProductCtrl.php
-model
-view
-includes
-components
-autoload.inc.php
-pages
-home.php
-add-product.php
-index.php
这是我的自动加载器函数:
spl_autoload_register(function ($class) {
$str = str_replace("\\", "/", $class);
$file = $str.".php";
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
index.php
<?php
require "includes/autoload.inc.php";
//home page
include "pages/Home.php";
现在,我可以从 home.php index.php ,但是每当我尝试访问 add-product.php 文件时,都会给我一个错误:
Fatal error: Uncaught Error: Class "classes\controller\ProductCtrl" not found in
project_directory\pages\add-product.php
note :我在包含文件夹结构的每个类中都使用了名称空间。
here is my folder structure:
-classes
-controller
-ProductCtrl.php
-model
-view
-includes
-components
-autoload.inc.php
-pages
-home.php
-add-product.php
-index.php
this is my autoloader function:
spl_autoload_register(function ($class) {
$str = str_replace("\\", "/", $class);
$file = $str.".php";
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
Index.php
<?php
require "includes/autoload.inc.php";
//home page
include "pages/Home.php";
Now I can access the ProductCtrl class from home.php which is in index.php, but whenever I try to accesss the same class in add-product.php file it gives me an error saying:
Fatal error: Uncaught Error: Class "classes\controller\ProductCtrl" not found in
project_directory\pages\add-product.php
Note: I have used namespace in every class containing the folder structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该文件很可能不存在,因此该类仍然不确定。
如果指定基本路径,则更明确,并且可能已经解决了:
还检查了“ auto-loader”处理哪些文件路径的通知日志(通过
trigger_error
记录,您稍后可以删除)。The file most likely does not exist and therefore the class remains undefined.
If you specify a base-path, it is more explicit and might solve it already:
Also check the error log for notices showing which file paths are processed by your auto-loader (by the
trigger_error
logging, you can remove it later).