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

Why not use include/require_once

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "Why not use include/require_once". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!

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:

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 returns. There is no continuation 2. Open the file and get the open path (opened path) 3. Take opened path to EG (included_files) to find whether it exists, and returns if it exists, but does not exist to 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:

Then, our b.php is placed in "/ tmp/b.php", which reads as follows:

Then when apc.include_once_override is opened, continuous access will get the following error:

Fatal error-include (): Cannot redeclare class b

(postscript 2012-09-15 02:07:20: I have repaired the bug of this APC: # 63070)

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.

That's all for "Why not use include/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