We know that in PHP, the same function name can not be used to define functions two times, if so, the program will be executed when the error.
And we're going to extract some of the most common custom functions into an include file, and then other files can call these functions via include or require, and here's an example:
<?php
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
may need to include additional files
Require ("test1.inc.php");
Require ("test2.inc.php");
Do no test
?>
The function of FUN1 is defined in both test1.inc.php and test2.inc.php, and I know that the functions implemented by these two functions are identical, but I'm not sure, or I don't want to know explicitly, whether a function is defined in a "package" (INCLUDE). Another problem is that we can't include a package two times, but I don't want to spend a lot of time checking it here, as in the example above, the execution of the test.php will produce many errors.
In C, predefined features are available to solve this problem:
#ifndef __fun1__
#define __fun1__
Do any thing
#endif
PHP does not provide such a mechanism, but we can take advantage of PHP's flexibility to achieve the same functionality as the C language reservation, as follows:
<?php
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
may need to include additional files
Require ("test1.inc.php");
Require ("test2.inc.php");
Do no test
?>
Now, we are no longer afraid to include a package multiple times or to define a function multiple times the error occurs. The good thing about this is that it makes it easier to maintain our programs. -->