For details about the efficiency of Repeated introduction of require, require_once, include, and include_once class libraries in php, requireonceinclude

Source: Internet
Author: User

For details about the efficiency of Repeated introduction of require, require_once, include, and include_once class libraries in php, requireonceinclude

First, let me explain in detail the four introduced functions.

Include ()AndRequire ()Same functions

The only difference is:Whether or not require () is executed or not, as long as it exists, php will be pre-introduced before execution,Include () is introduced only when the statement is executed.

 

Include_once ()AndRequire_once ()Check whether files are introduced. If files are introduced, they are not introduced.

The only difference is:Require_once ()It is unconditionally included, as the name implies, that is, it is stopped if an error is encountered after it is introduced,Include_once () is ignored and the execution continues.

 

I 'd like to explain the efficiency issues. Please use them less consciously.Include_once (),Require_once ()

 

Principle of this function: Introduce the file-> compare the current script statement to see if it has been included-> decide whether to introduce it. The efficiency can be imagined. If you want to use hundreds of class libraries throughout the project, what is the horrible consequence of comparing dozens of times in one execution?

 

I have seen some blog posts written by Daniel. I don't need to care about these small details, but I pay more attention to SQL optimization. My opinion is that for a programmer who wants to study continuously, we should keep a good habit of efficiency optimization, focusing on every detail

 

Here is how to prevent repeated file introduction in my multi-class libraries:

 

1. Use require () in the call script ();

 

2. Avoid repeated use of class_exists ('class name') or include ('class library absolute path') in the class library ');

 

Explanation: The file that calls the script uses require () once, because the call script is a general entrance of the program, the introduction of public class libraries will rarely causePublicThe class library is not used. Using the above statements in the class library can prevent the current script from repeatedly introducing the public class library, and the introduction will be executed only through judgment conditions, without repeated pre-introduction, improving the program execution efficiency.

 

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.