require_once 被忽略 [英] require_once ignored

查看:25
本文介绍了require_once 被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

windows 上 php 的奇怪问题...我的应用程序加载了一个核心"文件,该文件加载设置文件、注册自动加载、在核心文件顶部进行初始化等,我有 include_once("config.php");这适用于当前目录中的任何内容,如果我从一个单独的目录中包含核心文件,尽管它只是默默地忽略了配置文件的包含......以前有人见过这个吗?

strange problem with php on windows... my application loads a 'core' file that loads a settings file, registers autoloads, does initialization etc. at the top of the core file I have include_once("config.php"); this works fine for anything in the current directory, if I include the core file from a separate directory though it just silently ignores the include of the config file... has anyone seen this before?

/webroot/core.php

/webroot/core.php

<?php
  require_once "config.php";
  //register autoloads
  //do some initialization... standard stuff
?>

/webroot/config.php

/webroot/config.php

<?php
  define("WEB_ROOT","/webroot");
  define("DB_USER","root");
  // ... more stuff...
?>

/webroot/admin/index.php

/webroot/admin/index.php

<?php
  require_once("../dbconnect.php");
  echo WEB_ROOT;
  // print string literal WEB_ROOT rather than value in config.php
?>

我的理解是文件操作是相对于发出请求的文件所在的目录而言的,require_once("config.php")不应该选择相对于core.php的文件吗?这段代码就像我在 mac 或 Linux 上所期望的那样工作,但在 Windows 上却完全不同,(如果我更改了使用完整路径或 ../的要求,它可以工作)

My understanding is that file operations are relative to the directory of the file making the request, shouldn't the require_once("config.php") select the file relative to core.php? This code works just as I'd expect on a mac or Linux but not at all on windows, (if I change the require to use the full path or ../, it works)

真正疯狂的是 require_once("config.php") 没有抛出任何错误,但里面的代码没有被执行!

The real madness is that the require_once("config.php") doesn't throw any errors but none of the code inside is executed!

推荐答案

如果 PHP 默默地忽略错误,请尝试放在请求的文件的顶部:

If PHP silently ignore errors, try to put at the top of file that is requested:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

现在所有错误都应该可见.

Now all errors should be visible.

然后最佳实践(根据我的说法)是在请求的文件中(不在任何包含的文件中)为应用程序根定义一些具有完整文件系统路径的常量.所以在 index.php 中,如果应用程序根目录是 /webroot/admin:

Then the best practice (according to me) is to in file that is requested (not in any of included files) define some constant with full filesystem path for application root. So in index.php if application root is /webroot/admin:

define('APP_DIR', dirname(__FILE__));

之后,当您包含某些内容时,请使用此常量.如果目录结构是:

After that when you include something, use this constant. If the directory structure is:

/webroot/
    admin/
        index.php
    config.php
    core.php

并且您想将 config.php 包含到 index.php 中:

And you want to include config.php into index.php:

require_once APP_DIR . '/../config.php';

core.php 中包含 config.php 它将是:

In core.php to include config.php it would be:

require_once APP_DIR . '/../config.php';

等等.

使用绝对文件系统路径将防止任何歧义.

Using absolute filesystem paths will prevent any ambiguities.

这篇关于require_once 被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆