Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the reasons for not using include_once and require_once

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what are the reasons for not using include_once and require_once". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

To determine whether a file is loaded or not, PHP needs to get the opened_path of the file, 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

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.

This is the end of the content of "what are the reasons for not using include_once and require_once". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report