Php 包含的行为
这可能是一个愚蠢而简单的问题,但找不到谷歌的任何相关研究,也找不到我以前经历过的任何相关研究……为了复制该场景,我简化了原始结构,并设置了如下环境: 文件结构:Php 包含的行为,php,xampp,filenames,Php,Xampp,Filenames,这可能是一个愚蠢而简单的问题,但找不到谷歌的任何相关研究,也找不到我以前经历过的任何相关研究……为了复制该场景,我简化了原始结构,并设置了如下环境: 文件结构: ROOT |---/config/config.php |---/system/system.php index.php 在config/system.php中 return array ( "test" => "this is test" ); class config { public static
ROOT
|---/config/config.php
|---/system/system.php
index.php
在config/system.php中
return array (
"test" => "this is test"
);
class config
{
public static function site ()
{
return include "system.php";
}
}
include "config/config.php";
$result = config::site();
var_dump($result);
在config/config.php中
return array (
"test" => "this is test"
);
class config
{
public static function site ()
{
return include "system.php";
}
}
include "config/config.php";
$result = config::site();
var_dump($result);
在index.php中
return array (
"test" => "this is test"
);
class config
{
public static function site ()
{
return include "system.php";
}
}
include "config/config.php";
$result = config::site();
var_dump($result);
我希望$result
将包含system.php
中的数组,但它实际输出:int(1)
我猜它已经成功地包含了这个文件,这就是为什么它会输出1
,但这只是我的猜测
如果我将文件名从
/config/system.php
至/config/sys.php
return array (
"test" => "this is test"
);
class config
{
public static function site ()
{
return include "system.php";
}
}
include "config/config.php";
$result = config::site();
var_dump($result);
在/config/config.php
中,将此语句从
return包括“system.php”
to返回包括“sys.php”代码>
它将输出我想要的结果:
array(1) { ["test"]=> string(12) "this is test" }
虽然我找到了一个解决方法,但我不明白为什么文件名很重要,因为我认为php保留字的情况并非如此。此外,我尝试将文件命名为echo.php
或strpos.php
,但它仍能正常工作
为什么我不能将我的脚本命名为system.php
如果有问题的话,我的工作站使用的是xampp:Windows 64位,PHP5.6.8一个明显的问题,我应该投反对票;经典的RTM“小心地”。我再怎么强调我是多么的愚蠢。。。。。。。我的原始结构定义了一个绝对路径,但只在自动加载函数中使用,而不是在该类中使用
从:
文件是根据给定的文件路径包含的,如果没有给定文件路径,
指定的包含路径如果在中找不到该文件
include\u路径,include将最终检入调用脚本自己的
目录和失败前的当前工作目录。
所以我检查了include_路径,实际上是指向。;C:\xampp\php\PEAR
,在它下面,您猜对了,一个名为system.php
的文件
我的include
找到了正确的文件,但不是我期望的文件。它实际上包含了一个pear扩展文件,而不是我想要的脚本
我不知道也不记得在安装XAMPP时安装了pear扩展,也不知道XAMPP将其作为默认设置指向pear文件夹
总之,我需要将set_include_path()
设置为指向当前项目文件夹,或者使用绝对路径显式调用它
很抱歉浪费您的时间您得到了正确的输出。从PHP手册include,失败时返回FALSE并发出警告。成功包含,除非被包含的文件覆盖,否则返回1
@bansi-See:到目前为止,无法重现PHP5.5.9-1 Ubuntu的问题。与您的问题没有严格的关系,最好在include path之前附加\uuuu DIR\uuu
魔术常量,以防止执行DIR发生更改(例如,如果cron调用脚本)我完全同意你的看法,但是它不应该也把数组还给我吗??