Home > Article > Backend Development > Problems arising from repeated introduction in php
First of all, let me talk about these four imported functions in detail
include() and require() has the same functions
The only difference is: require() no matter whether it is executed or not, as long as it exists, php will be pre-introduced before execution, include() It is introduced only when the statement is executed
##include_once() and require_once() Both detect whether the file has been imported. If it is imported, it will not be imported.
The only difference is: require_once() is an unconditional inclusion. As the name suggests, it will stop if an error is encountered after being introduced. include_once() will ignore it and continue execution.
Regarding efficiency issues, I would like to explain that please consciously use less include_once(), require_once()
##Principle of this function: Import the file -> Compare the current script statement to see if it is included -> Decide whether to import it , the efficiency can be imagined. If you think about the entire project with hundreds of class libraries, what would be the horrific consequences of comparing dozens of times in one execution
I saw some big guys writing on their blogs, don’t worry about these small details, pay more attention to sql optimization, my opinion is, one wants to continue For serious programmers, they should maintain a good efficiency optimization habit and pay attention to every detail
Write here my personal solution to prevent repeated introduction of files in multi-class libraries:
1. Use require() in the calling script;
2. Prevent repeated use of class_exists('class name') or include('absolute path to class library');
Explanation: The file calling the script uses require() once, because the calling script is a general entry point for the program, and the public class library is introduced here It rarely happens that the public class library is not used. Using the above statement in the class library can prevent the current script from repeatedly introducing the public class library, and as long as it is passed The introduction will be executed only when the conditions are judged, and pre-introduction will not be repeated, which improves program execution efficiency
The above is the detailed content of Problems arising from repeated introduction in php. For more information, please follow other related articles on the PHP Chinese website!