PHP avoids duplicate declaration function solutions

zhaozj2021-02-17  52

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:

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:

?

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.

转载请注明原文地址:https://www.9cbs.com/read-28846.html

New Post(0)