In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "Why do not use include/require_once in php", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Why not use include/require_once in php" this article.
There has been a long discussion about whether to use include or include_once (below, including require_once), and the conclusion has always been that try to use include instead of include_once. The most common reason in the past is that include_once needs to query the list of loaded files to see if they exist before loading.
Admittedly, this reason is correct, but what I want to say today is another reason.
We know that PHP needs to get the opened_path of a file to determine whether a file is loaded, which means, for example:
The copy code is as follows:
When PHP sees include_once "2.php", he does not know what the actual path of the file is, and he cannot tell whether it has been loaded from the list of loaded files, so in the implementation of include_once, he will first try to parse the real path of the file (for ordinary files, this parsing is only similar to checking the getcwd and file path, so if it is a relative path Generally will not succeed), if the parsing is successful, then look for EG (include_files), if there is, it means that it has been included, return, otherwise open this file, thus get the opened_path of this file. For example, in the above example, this file exists in "/ tmp2/2.php".
Then, after getting the opened_path, PHP goes to the list of loaded files to find out whether it has been included, and if it does not, then directly compile, no longer need open file.
1. Try to parse the absolute path of the file. If the parsing is successful, check EG (included_files). If it exists, it will be returned. There is no continuation.
two。 Open the file and get the open path of the file (opened path)
3. Take opened path to EG (included_files) to find whether it exists. If it exists, it will be returned. If it does not exist, continue.
4. Compile file (compile_file)
This is not a problem in most cases, but the problem is when you use APC.
When using APC, APC hijacks the pointer to the compiled file compile_file, thus obtaining the compilation result directly from cache, avoiding the open of the actual file and the system call of open.
However, when you use include_once in your code, PHP has tried to open file before compile_file, and then enters the compile file hijacked by APC, which results in an additional open operation. In order to solve this problem, APC introduces include_once_override. When include_once_override is turned on, APC hijacks the ZEND_INCLUDE_OR_EVAL opcode handler of PHP, determines the absolute path of the file through stat, and then rewrites opcode to include to make a tricky solution if it is not loaded.
However, unfortunately, as I said, APC's include_once_override implementation has not been good, and there will be some undefined problems, such as:
The copy code is as follows:
Then, our b.php is placed in "/ tmp/b.php", which reads as follows:
The copy code is as follows:
Then when apc.include_once_override is opened, continuous access will get the following error:
Fatal error-include (): Cannot redeclare class b
Excluding these technical factors, I have always thought that we should use include instead of include_once, because we can fully plan for ourselves, and a file is only loaded once. This can also be done with the help of automatic loading.
When you use include_once, you can only prove that you have no confidence in your code.
So, I suggest you stop using include_once.
The above is all the content of the article "Why not to use include/require_once in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.