PHP to avoid repeating the declaration function solution

Source: Internet
Author: User

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. -->



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.