Home  >  Article  >  Backend Development  >  Detailed explanation of the efficiency problem of repeated introduction of class libraries in PHP

Detailed explanation of the efficiency problem of repeated introduction of class libraries in PHP

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-05-12 17:26:542084browse

This article will introduce to you the efficiency issues of repeated introduction of require, require_once, include, and include_once class libraries in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

include() has the same function as require()

The only difference is: regardless of whether require() is executed or not, as long as it exists, php will Pre-introduction, include() 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.

Unique Difference: 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 include_once() less , require_once()

Principle of this function: Introduce the file-> Compare the current script statement to see if it is included-> Decide whether to introduce it. The efficiency can be imagined. If you think you have hundreds of files in the entire project Class library, what kind of horrific consequences would it be to compare dozens of times in one execution?

Here is a solution to prevent repeated introduction of files in a personal multi-class library:

  • Use require() in the calling script;

  • 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 the general entrance of the program. The introduction of the public class library here will rarely cause the public class library not to be used. In the class library Using the above statement can prevent the current script from repeatedly introducing public class libraries, and the introduction will only be executed if the judgment conditions are passed, and the pre-introduction will not be repeated, improving the efficiency of program execution.

Recommended learning: php video tutorial

The above is the detailed content of Detailed explanation of the efficiency problem of repeated introduction of class libraries in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete