gpt4 book ai didi

php - __autoload 与命名空间类给找不到文件的 fatal error

转载 作者:行者123 更新时间:2023-12-04 17:04:27 30 4
gpt4 key购买 nike

我正在通过 __autoload特点 PHP提供。

然而这是index.php我有:-

define('app_path', realpath('../'));

$paths = array(
app_path, get_include_path());
set_include_path(implode(PATH_SEPARATOR, $paths));

function __autoload($classname)
{
$filename = str_replace('\\', '/', $classname.'.php');
require_once $filename;
}

use \engine\controllers as Controllers;

$data = new Controllers\base(); // This one is line no. 25 (Here is error)
echo $data->mind('Hi');

而这个是我的 base.php :-
 namespace controllers;
class base {
public function __construct() {
echo __CLASS__ . '<br/>';
echo __NAMESPACE__;
}

public function mind($myname)
{
echo $myname;
}
}

并抛出此错误:

enter image description here

我的目录结构如下:
app -> engine -> controller -> base.php app -> index.php
我不确定发生了什么问题。我只是在学习如何使用 namespace__autoload
我也试过 spl_autoload_register但没有成功。请建议。

编辑:1

另外,如果我想用 spl_autoload_register 替换它,如何实现。

最佳答案

不确定,但值得一试:

在 base.php 中,更改为 namespace engine\controllers;在第 1 行。

在 index.php 中,更改为 use engine\controllers as Controllers; (删除前导反斜杠)在第 23 行。

关于php - __autoload 与命名空间类给找不到文件的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17086756/

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