Again, do not use (include/require) _ once

Source: Internet
Author: User
Tags autoload apc apc configuration
Author: Laruence () address of this article: www.laruence.com201209122765.html reprinted please indicate the source of recent apc. when we made several discussions about the destde_once_override, this APC configuration item has never been well implemented. here, I want to share with you the reasons for this problem,

Author: Laruence () address of this article: http://www.laruence.com/2012/09/12/2765.html reprint please indicate the source of recent apc. when we made several discussions about the destde_once_override, this APC configuration item has never been well implemented. here, I want to share with you the reasons for this problem,

  • Author: Laruence ()
  • Address: http://www.laruence.com/2012/09/12/2765.html
  • Reprinted please indicate the source

We have made several discussions about the removal of apc. include_once_override. This APC configuration item has never been well implemented.

Here, I would like to share with you the reasons for this problem and some inspiration for us.

We have been discussing whether to use include or include_once (all of the following include require_once) for a long time. The conclusion is that we should try to use include instead of include_once, include_once needs to query the list of loaded files, check whether the file exists, and then load the file.

It is true that the reason is correct. However, what I want to say today is another reason.

We know that PHP needs to obtain the opened_path of the file to determine whether a file is loaded, which means, for example:

 

When PHP sees include_once "2. php ", he does not know the actual path of the file, and cannot judge whether the file has been loaded from the loaded file list. Therefore, in the implementation of include_once, will first try to parse the actual path of this file (for a common file, this resolution is just like checking the getcwd and file path, so if it is a relative path, it is generally not successful), if the resolution is successful, search for EG (include_files). If the file exists, it indicates that it has been included. Otherwise, open the file to obtain the opened_path of the file. for example, in the above example, this file exists in "/tmp2/2. php ".

Then, after obtaining the opened_path, PHP goes to the loaded file list to check whether the file is included. If the file is not included, compile the file directly without the open file.

1. try to parse the absolute path of the file. If the file can be parsed successfully, check for EG (included_files). If the file exists, return. If the file does not exist, continue. 2. open the file and obtain the open path (opened path) of the file. 3. use opened path to go to EG (included_files) to check whether the file exists. If the file exists, the file is returned and the file does not exist. 4. compile a file (compile_file)

This is not a problem in most cases, but the problem is when you use APC...

When APC is used, APC hijacks the pointer to the compile_file compilation file to directly obtain the compilation result from the cache, avoiding open to the actual file, this avoids system call for open.

However, when you use include_once in the code, PHP has tried to open file before compile_file, and then enters the compile file hijacked by APC, an additional open operation is generated. to solve this problem, APC introduces include_once_override. When include_once_override is enabled, APC will hijack PHP's ZEND_INCLUDE_OR_EVAL opcode handler and use stat to determine the absolute path of the file, then, if it finds that it is not loaded, rewrite opcode to include and create a tricky solution.

However, it is a pity that, as I said, the implementation of the include_once_override of APC has always been poor, and some undefined problems may occur, such:

 

Then, we place B. php In "/tmp/B. php". The content is as follows:

 

When apc. include_once_override is enabled, the following error is returned for continuous access:

Fatal error - include() : Cannot redeclare class b

(Note: 02: 07: 20: I have fixed this APC bug: #63070)

To eliminate these technical factors, I have always believed that we should use include instead of include_once, because we can completely implement our own planning, and a file is loaded only once. you can also achieve this by means of automatic loading.

You use include_once to prove that you have no confidence in your code.

Therefore, we recommend that you do not use include_once

Comments
  • , Haoboys writes: always not applicable to include/require_once seems wise
  • , Peanut writes
  • , Shaukei writes: laruence, taught!
  • , Shiran writes: include_once uses an absolute path. Can this problem be avoided in the example?
  • 2012/09/12, test writes: This only indicates that the require_once mechanism or APC should be updated.
  • , Jason Gian writes: taught, laruence
  • , Maker writes: learning, so advanced.
  • , Pysche writes: This code is completely fine for your own ability to control, but for some projects, I personally think it is very difficult. For example, for Wordpress plug-ins, various plug-ins are developed by different people, with different levels.
  • 2012/09/12, heyli writes: but the problem is that when you use APC, that is to say, my program does not use apc include_once?
  • , Zzjin writes: laruence... the second include in the third line is spelled wrong... amount
  • , Snow migratory bird writes: @ zzjin Haha, I have already corrected it. I am a bigint ....:)
  • 2012/09/12, tiger writes: agree to modify the once mechanism or apc's once mechanism.
  • , Jianghu prawn writes: I still don't quite understand why. The best case is, of course, to avoid repeated loading through a reasonable architecture. If you only need to maintain an array by yourself to determine whether or not the array has been loaded, then there will be no repeated de_once. I think pysche is right. Sometimes I can only use include_once to write the entire project.
  • , Danger writes: learning, good.
  • , Luke writes: reserved name... I was taught .. Passing...
  • , Once again, do not use (include/require) _ once-extraordinary past writes: [...] Original: http://www.laruence.com/2012/09/12/2765.html [...]
  • 2012/09/12, fc_lamp writes: good ~~ Mark the Bird brother reprinted the signature.
  • , Littlemiaor writes: Haha never used once
  • , Wizardmin writes, especially when a class is used cyclically, the import array must be retrieved every time. Generally, the include/require_once solution is used.
  • , Ye Fengfeng writes: I think most of them are lazy to use include_once, so they are too lazy to judge. In fact, if a project is developed by one person and maintained by one person, this can be completely avoided. If a group is used for multi-person cross-development, or after a period of time, it will be maintained by another person, in particular, when the levels of maintainers are different, the advantages of include_once emerge. After all, many large programs, large files, and many files are packaged, it is complicated or even painful to know whether a file has been packaged. With this statement, it is much easier. It varies from person to person, depending on the local conditions, select as appropriate.
  • , Alvind writes
  • 2012/09/12, icecream writes: Will this cause performance degradation? I still cannot convince myself that I don't need it.
  • , Huushen222 writes: I have not seen any warning information for the same code. PHP 5.3.2-1ubuntu4. 17 with Suhosin-Patch (cli) (built: Jun 19 2012 01:35:33) APC Version: 3.1.3p1 apc. enable_cli = Onapc. include_once_override = On
  • , Huushen222 writes. ChromeError 324 (net: ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
  • 2012/09/12, imbiss writes: What if Zend autoload is used?
  • 2012/09/13, Chon writes: include_once_override is not implemented well and should not be used (include/require) _ once. This is a problem with include_once_override. it is recommended that the primary account should only be: include_once_override and once should not be used at the same time. PHP itself does not have strong support in terms of modularity and namespace, And it is basically difficult to solve it by using autoload. if you do not use PHP, it will only apply to some small sites.
  • , Haina baichuan writes: learning. I don't know much about it.
  • , Once again, do not use (include/require) _ once forest/coffee Chengdu professional php website production | forest/coffee Chengdu professional php website production writes: [...] snow? PHP application Posted in: php/Tagged: Do not use include/require_once, and then [...]
  • 2012/09/14, a wonderful story-a little novel about literature reading xlinblog.sinaapp.com again, do not use (include/require) _ once writes: [...] This article address: http://www.laruence.com/2012/09/12/2765.html [...]
  • 2012/09/14, quiet writes: in theory, once is not used. Actually, it is reasonable because there are many authors who think about open-source code. For example, when phpcms is generated, they have to go through foreach ($ a as $ B) {include_once ("B. php ");} has to use include_once
  • 2012/09/14, Kevin. yt90 writes: Yes. Note later
  • 2012/09/16,» again, do not use (include/require) _ once writes: [...] original address :? Http://www.laruence.com/2012/09/12/2765.html [...]
  • , Writes: once is necessary when the project is complex and the team level is uneven.
  • , Stone writes: always use the following code to replace require_once
  • , Stone writes: Er, the code was filtered out... Function myrequire ($ file) {static $ loaded = array (); if (in_array ($ file, $ loaded) return; $ loaded [] = $ file; return require ($ file );}
  • 2012/09/19, cevin writes: Can I say I agree seriously. Never use include_once or require_once
  • , Anru writes: This is really far-fetched. First, how fast is include than include_once? Several milliseconds. Second, this should be the error of APC, which is about include_once. Include_once is faster than include when using an absolute path name. Please refer to the two discussions. ZendFrame work, drupal, Sugarcrm, WordPress, symfony framework, these popular PHP applications all use require_once/include_once "to eliminate these technical factors. I have always thought that we should use include, instead of include_once, because we can completely implement our own planning, a file is loaded only once. this can also be achieved through automatic loading. "For a programmer, why is it not lazy (using tools to improve efficiency )? If you have to plan everything, simply create your own CPU.
  • , Lamp white writes: I only knew that once is not recommended, because I had to make multiple judgments, but I learned more after reading it.
  • 2012/09/29, zencart writes: Use require instead of include ..
  • , Anonymous writes: I also want to say that it should be an apc problem. You don't need apc.
  • , Ogawa writes: technology is really important, but don't turn it into a technology.
  • June 20,: writes, a QR code farmer, was shocked. It took several years to use include_once. You have never heard of this problem. It is reasonable to exist.
  • , Do not use include_once/require_once | speed note writes: [...] source reference from: http://www.laruence.com/2012/09/12/2765.html this entry is published in the PHP classification directory, pasted with the include, include_once, require, require_once tag. Add a fixed link to favorites. ← CodeIgniter (CI) Long-title Chinese garbled solution [...]
  • , About PHP compilation and execution separation | 5 iphp writes: [...] Again, do not use (include/require) _ once [...]
  • , Huming17 writes: require_once is still necessary. When an absolute path loads a file, it takes a short time to query the list of loaded files. However, loading PHP files consumes a lot of time, I think require_once is better than require.
  • , Hua dingping writes: What is APC? I am dull
  • , Hello writes: This sentence is classic: You use include_once to prove that you have no confidence in your code. by the way, it is classic: you do not use absolute paths for include. It only proves that you do not know the environment where your code is located.
  • , Minecraftskins writes:
  • , Leric writes: new projects should be loaded automatically. If you write the include statement everywhere, it will take a long time to learn it, but you may need to pay attention to it when using APC for old projects.
  • 2013/04/09, a clear understanding of require and include | Maverick writes: [...] the difference between require and require_once is that require contains a file once. If another file is included in the subsequent programming, PHP will still find the file, parse the file, and execute the file, require_once contains a file. If a file already contains require_once is included in later programming, the file will be ignored. Instead of searching for the file, the file will be read directly to the contained memory information. I personally think that require_once can be used in a global location, and require can be used in a more personalized location. The global file may be contained multiple times during multi-person development, require is generally used for personal personalization. Most people in their programs know what files they contain. for php laruence, we recommend using include or require when using apc cache. We do not recommend _ once operations .? Http://www.laruence.com/2012/09/12/2765.html [...]
  • , In-depth study $» again, do not use (include/require) _ once writes: [...] This article address: http://www.laruence.com/2012/09/12/2765.html [...]
  • , Kknd li writes: the main reason for the existence of once is that PHP has very few project constraints on developers in its early development practices and is relatively simple. After the introduction of object-oriented programming, although the automatic loading mechanism is available, it is still implemented by developers and has not been improved or supported by early functional programming. For many developers, This is a burden. In fact, PHP is not as convenient and lightweight as it was at the beginning. It is complicated and a little fragile. With the complexity of the project, developers are in a steep and increasing process. When the project is slowly maintained by patchwork, when people around you rely on _ once to ensure that you do not load them repeatedly and you are powerless, you have to say what it feels like to say that the language is full.
  • , Once again, do not use (include/require) _ once | afternoon nap writes: [...] This article address: http://www.laruence.com/2012/09/12/2765.html [...]
  • , About Yaf | afternoon nap writes: [...] Again, do not use (include/require) _ once [...]
  • 2013/05/19, Ill tell you some sneak preview along with my favorite comfortable shoes origina writes: Khmer Comedy» Somnangblogs I was proposed this internet site by my own cousin. I am not sure whether this post is published by him since nobody in addition know this sort of detailed with regards to my trouble. you're incredible! Thanks! Your write-up about Khmer Humourous» Somnangblogs All the best Lisa Veronica
  • , Roger writes: Sometimes you don't have confidence in your own code. Instead, you have to take over others' code to ensure security.
  • , Xsir317 writes: how do I feel that APC has used a lot of HACK methods...
  • , Wsq writes: You may add a parameter to require_once to limit the open file frequency.
  • , Shanghai weather forecast writes: it has been repeatedly stressed that the model of teammates is not changed, and sometimes it is really tiring for programmers who have cleanliness ..
  • , Soooldier writes: My understanding is the opposite. If you leave the repetitive file inclusion work to a programmer, you cannot avoid creating a set of classes similar to "loaded. I think it is more appropriate to judge the code that has been loaded from the bottom layer. After all, if a programmer writes one by himself, it will eventually be parsed into opcode through various parsing methods, this efficiency should be lower than the underlying C implementation. As for the impact of APC on include_once (require_once), this is an implementation problem of APC and should not be attributed to the use of include_once (require_once.
  • 3rd, soooldier writes: laruence is not familiar with Step 1 in the include_once process description: Why do I need to query included_files again?
  • 3rd, soooldier writes?
  • , Talking about blog writes: With namespace, do you not have to consider include, require, automatic import?
  • 2014/05/04, How To Set Up A Website writes: Great article.
  • 2014/05/14, Verses writes: what is the meaning of using the name below to explain? Is it better than include?
  • 2014/07/20, consatan writes: 1. try to parse the absolute path of the file. If the file can be parsed successfully, check for EG (included_files). If the file exists, return. If the file does not exist, return, so why call include_once ('/path/to/file in function. php ') will cause external import of the same file to fail? Function a () {include_once ('/path/to/file. php '); var_dump ($ var);} a (); // output successinclude_once ('/path/to/file. php '); var_dump ($ var); // If the output NULL "exists, return" only indicates that "the introduced file already exists" and then does not introduce it ??
  • , Excessive require () in php, will this lead to excessive execution time? | Segment-solution writes: [...], recommended by the PHP developer laruence to avoid using ------------------------------------------------------------------------------- [...]
Copyright©2010 All Rights Reserved. this Feed is for personal use only. reposted or commercial applications that are not specified are prohibited. if the application is illegal, all legal consequences shall be borne by you. if you have any questions, you can send an email to my at laruence.com. (Digital Fingerprint: 73540ba0a1738d7d07d4b6038d5615e2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.