Please leave include_once and require_once_php tips

Source: Internet
Author: User

True, the reason is right, but what I am going to say today is another reason.
We know that PHP to determine whether a file is loaded, it is necessary to get the opened_path of this file, meaning that, for example:

Copy Code code as follows:

<?php
Set_include_path ("/tmp/:/tmp2/");
Include_once ("2.php");
?>

When PHP saw Include_once "2.php", he did not know the actual path of the file, and can not from the list of loaded files to determine whether it has been loaded, so in the implementation of Include_once, will first attempt to parse the real path of the file ( For ordinary files this resolution is just similar to check getcwd and file path, so if it is relative path, generally will not be successful, if the resolution is successful, then look for eg (include_files), if there is a description contains, return, otherwise open this file, Thus getting the opened_path of this file. For example, this file exists in "/tmp2/2.php".

Then, after getting this opened_path, PHP goes to the list of loaded files to find, whether it already contains, if not included, then directly compile, no longer need open file.

1. Attempt to parse the absolute path of the file, if it can be resolved successfully, then check eg (included_files), the existence is returned, does not exist to continue
2. Open the file and get the file open path (opened path)
3. Take opened path to eg (included_files) to find if there is, if present, return, 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 ...

In the use of APC, APC hijacked compile_file this compiled file pointer, so directly from the cache to get the results of the compiler, to avoid the actual file open, to avoid the open system call.

However, when you use include_once in your code, PHP tries to go to open file before Compile_file, and then into compile file hijacked by APC, which results in an extra open operation. And APC is to solve this problem, introduced Include_once_override, in the case of Include_once_override Open, APC will hijack php zend_include_or_eval opcode Handler, through stat to determine the absolute path of the file, and then if found not to be loaded, rewrite opcode for include, make a tricky solution.

However, unfortunately, as I said, APC's include_once_override has been implemented poorly, there will be some undefined questions, such as:

Copy Code code as follows:

<?php
Set_include_path ("/tmp");
function A ($arg = Array ()) {
Include_once ("b.php");
}

A ();
A ();
?>

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

<?php
Class B {}
?>

In the case of open apc.include_once_override, 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, not include_once, because we can do our own planning, a file is only loaded once. You can also use automatic loading to do this.

You use include_once only to prove that you have no confidence in your own code.
Therefore, we suggest that you do not use include_once

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.