PHP solution to avoid repeating the function _php tutorial

Source: Internet
Author: User
PHP avoids repeating the function solution Jincoo (from Ruted.com's crawler) we know that in PHP you can't use the same function name to define functions two times, and if so, the program executes with an error. And we will extract some of the usual custom functions into an include file, and then other files can call these functions through include or require, here is an example: 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 any test
?> defined fun1 in both test1.inc.php and test2.inc.php, and I know that the functions are exactly the same, but I'm not sure, or I don't want to know for certain whether a function is in a "package" (INCLUDE) , another problem is that we cannot include a package two times, but I do not want to spend too much time checking here, the above example, the execution of test.php will produce a lot of errors. In the C language, 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 function as the C language, the following examples:
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 any test
?> now, we are no longer afraid to include a package multiple times or define a function multiple times the error occurred. The immediate benefit of this is that it is easier to maintain our programs.

http://www.bkjia.com/PHPjc/532336.html www.bkjia.com true http://www.bkjia.com/PHPjc/532336.html techarticle PHP avoids repeating the function solution Jincoo (from Ruted.com's crawler) we know that in PHP you can't use the same function name to define functions two times, if so, when the program executes ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.