PHP avoids duplicate declaration function solutions
Jincoo (crawler from Ruted.com)
We know that the same function name cannot be used in PHP twice, if so, the program will be wrong.
And we will extract some commonly used custom functions, put them in an Include file, then other files can call these functions through include or Require, the following is an example:
php // file name test1.inc.phpfunction fun1 () {// do any fun1} function fun2 () {// do any fun2}?>
// file name test2.inc.phpRequire ("test1.inc.php"); function fun1 () {// do any fun1} function fun3 () {// do any fun3}?>
// file name test.php // may need to include other files Require ("Test1.inc.php"); Require ("Test2.inc.php"); // do any test?>
In Test1.inc.php and Test2.inc.php, the FUN1 function is also defined, I know that the functions implemented by these two functions are identical, but I am not sure, or I don't want to know, a function is Not defined in a "package", another problem is that we can't include a package twice, but I don't want to spend too much time here, the above example, perform Test.php There will be a lot of errors.
In the C language, it provides a predefined feature to solve this problem:
#ifndef __fun1 __ # define __fun1 __ // do anything # ENDIF
PHP does not provide such a mechanism, but we can use PHP flexibility, implementation and predetermined features of C language, as follows:
?
// file name test2.inc.phpRequire ("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 // may need to include other files Require ("Test1.inc.php"); Require ("Test2.inc.php"); // do any test?>
Now, we are no longer afraid to include a package multiple or define a function that will appear multiple times. This is the advantage that the benefits directly bringing us is that it is relatively easy to maintain our procedures.