【问题标题】:PHP: Class not found in subdirectoryPHP:在子目录中找不到类
【发布时间】:2021-05-01 12:06:10
【问题描述】:

我有以下文件夹结构:

dbh.class.php 连接到数据库,test.class.php 从我的数据库中获取数据。当我在我的 index.php 中并使用以下代码时:

include_once 'classes/dbh.class.php';
include_once 'classes/test.class.php';
$testobjekt = new Test();
$testobjekt->getUsers();

我得到了我想要的结果。

但是当我尝试在子目录 public/home.php 中使用相同的代码调用类及其方法时,我得到一个错误:

Warning: include_once(/opt/lampp/htdocs/funktionstest/public/classes/dbh.class.php): Failed to open stream: No such file or directory in /opt/lampp/htdocs/funktionstest/public/home.php on line 4

当我想从子目录调用类时,我做错了什么?我用绝对路径include_once __DIR__ . '/classes/test.class.php';试过了,还是不行。

【问题讨论】:

  • 警告应该是一个赠品 - 它显示了它试图找到它的路径:funktionstest/public/classes/。相对包含(例如您的)总是从当前脚本的目录中搜索。

标签: php directory include


【解决方案1】:

home.php 在公共目录中。所以你需要添加..
..表示父目录。

试试:

include_once '../classes/dbh.class.php';
include_once '../classes/test.class.php';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2019-04-26
    • 2018-12-31
    • 2012-05-15
    • 2014-01-17
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多