PHP避免重复申明函数的解决方案_PHP教程
程序员文章站
2022-04-13 15:19:19
...
PHP避免重复申明函数的解决方案jincoo(来自RUTED.COM的爬虫) 我们知道,在PHP中不能使用相同的函数名定义函数两次,如果这样,程序执行的时候就会出错。 而我们会把一些常用的自定义函数提取出来,放到一个Include文件中,然后别的文件就可以通过Include或require来调用这些函数,下面是一个例子:// File name test1.inc.php
function fun1()
{
// do any fun1
}
function fun2()
{
// do any fun2
}
?>
// File name test2.inc.php
require("test1.inc.php");
function fun1()
{
// do any fun1
}
function fun3()
{
// do any fun3
}
?>
// File name test.php
//可能需要包含其他的文件
require("test1.inc.php");
require("test2.inc.php");
// do any test
?> 在test1.inc.php和test2.inc.php中同时定义了fun1这个函数,我虽然知道这两个函数实现的功能完全相同,但是我并不确定,或者说我不想明确的知道,一个函数是不是在某个“包”(INCLUDE)中定义了,另外的一个问题是,我们不能包含一个包两次,但是我并不想在这里花过多的时间进行检查,上面的例子,执行test.php会产生很多错误。 在C语言中,提供了预定义功能可以解决这个问题:#ifndef __fun1__
#define __fun1__
// do any thing
#endif PHP并不提供这样的机制,但是我们可以利用PHP的灵活性,实现和C语言的预定一同样的功能,下面举例如下:
// File name test1.inc.php
if ( !isset(____fun1_def____) )
{
____fun1_def____ = true;
function fun1()
{
// do any fun1
}
}
if ( !isset(____fun2_def____) )
{
____fun2_def____ = true;
function fun2()
{
// do any fun2
}
}
?>
// File name test2.inc.php
require("test1.inc.php");
if ( !isset(____fun1_def____) )
{
____fun1_def____ = true;
function fun1()
{
// do any fun1
}
}
if ( !isset(____fun3_def____) )
{
____fun3_def____ = true;
function fun3()
{
// do any fun3
}
}
?>
// File name test.php
//可能需要包含其他的文件
require("test1.inc.php");
require("test2.inc.php");
// do any test
?> 现在,我们不再怕同时包含一个包多次或定义一个函数多次会出现的错误了。这样直接带给我们的好处是,维护我们的程序变得比较轻松了。
function fun1()
{
// do any fun1
}
function fun2()
{
// do any fun2
}
?>
// File name test2.inc.php
require("test1.inc.php");
function fun1()
{
// do any fun1
}
function fun3()
{
// do any fun3
}
?>
// File name test.php
//可能需要包含其他的文件
require("test1.inc.php");
require("test2.inc.php");
// do any test
?> 在test1.inc.php和test2.inc.php中同时定义了fun1这个函数,我虽然知道这两个函数实现的功能完全相同,但是我并不确定,或者说我不想明确的知道,一个函数是不是在某个“包”(INCLUDE)中定义了,另外的一个问题是,我们不能包含一个包两次,但是我并不想在这里花过多的时间进行检查,上面的例子,执行test.php会产生很多错误。 在C语言中,提供了预定义功能可以解决这个问题:#ifndef __fun1__
#define __fun1__
// do any thing
#endif PHP并不提供这样的机制,但是我们可以利用PHP的灵活性,实现和C语言的预定一同样的功能,下面举例如下:
// File name test1.inc.php
if ( !isset(____fun1_def____) )
{
____fun1_def____ = true;
function fun1()
{
// do any fun1
}
}
if ( !isset(____fun2_def____) )
{
____fun2_def____ = true;
function fun2()
{
// do any fun2
}
}
?>
// File name test2.inc.php
require("test1.inc.php");
if ( !isset(____fun1_def____) )
{
____fun1_def____ = true;
function fun1()
{
// do any fun1
}
}
if ( !isset(____fun3_def____) )
{
____fun3_def____ = true;
function fun3()
{
// do any fun3
}
}
?>
// File name test.php
//可能需要包含其他的文件
require("test1.inc.php");
require("test2.inc.php");
// do any test
?> 现在,我们不再怕同时包含一个包多次或定义一个函数多次会出现的错误了。这样直接带给我们的好处是,维护我们的程序变得比较轻松了。
上一篇: 请问一个PHP安装程序的小疑点
下一篇: PDO exec update
推荐阅读
-
解析PHP函数array_flip()在重复数组元素删除中的作用
-
PHP-X系列教程之内置函数的使用示例
-
浅析PHP关键词替换的类(避免重复替换,保留与还原原始链接)
-
php数组函数序列之array_unique() - 去除数组中重复的元素值
-
PHP去除数组中重复的元素并按键名排序函数
-
PHP操作MySQL的mysql_fetch_* 函数的常见用法教程
-
深入file_get_contents函数抓取内容失败的原因分析_PHP教程
-
字符串长度函数strlen和mb_strlen的区别示例介绍,strlenmb_strlen_PHP教程
-
PHP递归调用数组值并用其执行指定函数的方法,递归数组_PHP教程
-
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解_PHP教程