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.