PHP solution to avoid repeated declarations of functions

Source: Internet
Author: User
PHP solution to avoid repeated declarations of functions jincoo (from RUTED. COM crawler) we know that the same function name cannot be used in PHP to define the function twice. if so, an error occurs during program execution. Some common user-defined functions are extracted and put into an Include file, and then SyntaxHighlighter. all

PHP solution to avoid repeated declarations of functions jincoo (from RUTED. COM crawler) we know that the same function name cannot be used in PHP to define the function twice. if so, an error occurs during program execution. Some common user-defined functions will be extracted and put into an Include file, and other files can call these functions through Include or require. The following 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
// Other files may need to be included.
Require ("test1.inc. php ");
Require ("test2.inc. php ");
// Do any test
?> In test1.inc. php and test2.inc. the function fun1 is defined in php at the same time. although I know that the functions of these two functions are identical, I am not sure, or I don't want to know clearly, is a function defined in a "package" (INCLUDE)? another problem is that we cannot INCLUDE a package twice, however, I don't want to spend too much time checking. in the above example, execute test. php produces many errors. In C, the predefined function is provided to solve this problem: # ifndef _ fun1 __
# Define _ fun1 __
// Do any thing
# Endif PHP does not provide such a mechanism, but we can use the flexibility of PHP to implement the same features as the C language reservation. The following is an example:
// 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
// Other files may need to be included.
Require ("test1.inc. php ");
Require ("test2.inc. php ");
// Do any test
?> Now, we are no longer afraid of errors that may occur when a package is included multiple times or when a function is defined multiple times. This directly brings us the advantage that it is 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.