php 5.4是否忽略includes中定义的()?
不确定这是否是由于我安装的模块造成的,我已尝试删除所有扩展,但仍然无法正常工作:php 5.4是否忽略includes中定义的()?,php,Php,不确定这是否是由于我安装的模块造成的,我已尝试删除所有扩展,但仍然无法正常工作: //test1.php if(defined("TEST1")) { return; } define("TEST1",1); function test() {} //test2.php if(defined("TEST1")) { return; } define("TEST1",1); function test() {} //test.php include_once('
//test1.php
if(defined("TEST1")) {
return;
}
define("TEST1",1);
function test() {}
//test2.php
if(defined("TEST1")) {
return;
}
define("TEST1",1);
function test() {}
//test.php
include_once('test1.php');
include_once('test2.php');
test();
导致重复的定义错误。看起来像函数_exists这样的其他检查也会起作用,但使用起来有点混乱。根据PHP文档(): 函数在被引用之前不需要定义,除非函数是有条件定义的 这意味着,如果不将test()函数放入条件语句中,它将在脚本执行开始之前定义 为了允许引用代码中进一步定义的函数,PHP首先搜索文件中的函数(类等)定义,然后运行代码。因此,当您在进行以下操作时:
if(defined('TEST1')) return;
Te函数已存在,并触发复制错误。解决方法是将它们放在任何条件语句中(不一定有意义),甚至放在大括号中。以这种方式定义的函数在脚本执行之前不会被定义,而且在定义之前也不能使用它们。只需执行以下操作,即可修复代码:
//test1.php
if(defined("TEST1")) {
return;
}
define("TEST1",1);
{
function test() {}
}
//test2.php
if(defined("TEST1")) {
return;
}
define("TEST1",1);
{
function test() {}
}
//test.php
include_once('test1.php');
include_once('test2.php');
test();
要测试行为,可以使用这两个代码段。这一个将起作用:
<?php
test();
function test() {
echo 'Hello world!';
}
<?php
{
function test() {
echo 'Hello world!';
}
}
test();
试试看
<?php
{
function test() {
echo 'Hello world!';
}
}
test();
//test1.php
if(!defined("TEST1")) {
define("TEST1",1);
function test() {}
}
//test2.php
if(!defined("TEST1")) {
define("TEST1",1);
function test() {}
}
//test.php
include_once('test1.php');
include_once('test2.php');
test();