【问题标题】:PHP include_once fails without warning or error for an existing filePHP include_once 失败,对现有文件没有警告或错误
【发布时间】:2016-06-16 20:42:43
【问题描述】:

给我一​​个错误,这让我很困惑。我正在尝试在 php 中实现自定义类加载器。我在 5.6.11 版上。问题是我正在动态构建文件的路径,然后在该路径上执行“include_once”。 PHP 只是在此时终止执行,没有警告或错误消息。路径有效。代码基本是这样的

require_once "lib/includes/web/classloader/default.php";
require_once "lib/includes/web/controller/default.php";
$classloader = new web\classloaders\ClassLoader();
$controller_path = $classloader->registerController();
if($controller_path != null){
    require_once("$controller_path");
}

file_exists 和 is_readable 检查在类加载器的 registerController() 函数中完成。

我也试过这个:

require_once $controller_path;

还有这个:

require_once($controller_path);

有什么想法吗?

问候

【问题讨论】:

  • require_once (dirname(FILE) . '/default.php');
  • 我在 Windows 10 上使用 xampp
  • 我是命名空间的新手,但这不是因为你的 $classloader 在另一个命名空间中吗?你有没有检查你的文件名是否有大写,种类 default.php -> Default.php ?
  • 类似 Linux 的路径在 Windows 中是否有效?
  • 您是否检查过 apache 日志中的错误,而不仅仅是屏幕上可能出现的错误?

标签: php model-view-controller controller require-once


【解决方案1】:

我用代码测试过,语法没有问题。问题应该在路径上。在使用 require_once 查看路径之前尝试使用echo $controller_path; die();

你可以插入

error_reporting(E_ALL); ini_set("display_errors", 1);

在 PHP 页面的开头启用错误显示,这样您就可以看到导致错误的原因..

【讨论】:

  • 再次感谢您的帮助。错误出现在使用的命名空间中。我设法将问题的根源追溯到我没有正确命名空间的类之一。 Xenofexs 发布您的提示,我会接受。
猜你喜欢
  • 2018-03-01
  • 2018-03-26
  • 2011-03-16
  • 2018-08-30
  • 2022-10-16
  • 2013-10-27
  • 2014-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多