The two most common functions for PHP programmers are require_once and include. through these two functions, we can use the classes and other objects defined in other class libraries. However, many users simply use the following code to reference other files in the same directory.
Include performance
The code is as follows:
Include ('include. php ');
Of course, this method is not wrong, but it is slightly less efficient than the following method:
The code is as follows:
Include (realpath (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'include. php ');
In this way, we may need to enter more information, but compared with the previous method, we need the PHP engine to iterate through de_path to find all the names 'include. php' can find the corresponding object. the absolute path dirname (_ FILE _) allows the system to quickly locate the corresponding FILE.
In PHP, constant _ FILE _ is very similar to AppDomain. CurrentDomain. BaseDirectory in C #. it returns the absolute path of the FILE where the code being executed is located. The dirname () function returns the path of its parent folder.
Another method that is more efficient in searching and easy to write is include ('./include. php'), which is equivalent to telling the system to find the 'include. php' file in the current path.
In large systems, we often use another better method. we often add the following code to the routing file or other initialization files:
The code is as follows:
Define ('app _ path', realpath (dirname (_ FILE _)));
This is equivalent to adding a global variable to the system to point out the system root directory. when we need to reference a file in a specific path, we can use the following code:
The code is as follows:
Include (APP_PATH.DIRECTORY_SEPARATOR. 'models'. 'user. php ');
Performance Comparison between autoload and include
For example, there are four scripts:
The code is as follows:
# File: include1.php
Include 'include2. php ';
// @ Todo something # file: include2.php
// @ Todo something # file: script1.php
Include 'include2. php ';
// @ Todo something
# File: script2.php
Include 'include1. php ';
Include 'script1. php'
// @ Todo something
When script1.php is executed, include 'include2. php'; this line of code is executed once. When script2.php is executed, this line of code is executed twice.
Here is just a simple example. in actual projects, include2.php may be included more times. Will repeated include operations affect performance? For this reason, I wrote a script for testing.
The code is as follows:
# File: SimpleClass. php
Class SimpleClass {
Public function _ construct (){
Echo get_time (). "rn ";
}
}
# File: php_include.php
For ($ I = 0; $ I <$ loop; $ I ++ ){
Include_once "SimpleClass. php ";
New SimpleClass ();
}
When $ loop is set to 1, the script takes about 0.00018906593322754 seconds. when $ loop is set to 1000, the script takes about 0.076701879501343 seconds.
What if we use autoload for implementation?
The code is as follows:
# File: php_autoload.php
Function _ autoload ($ class_name ){
Include_once $ class_name. '. php ';
} For ($ I = 0; $ I <$ loop; $ I ++ ){
New SimpleClass ();
}
In this code, I defined the _ autoload function, which is almost the same script. when $ loop is 1, it takes 0.0002131462097168 seconds, while when $ loop is 1000, it takes only 1/7 to 0.012391805648804 seconds for the previous code.
But please note that the SimpleClass code outputs a line of strings. what kind of results will be compared after this line of output is removed?
When $ loop is both 1000, it takes 0.057836055755615 seconds, and after autoload is used, it takes only 0.00199294090271 seconds! Efficiency is nearly 30 times different!
From the test above, we can see that when the file is only included once, autoload will consume a little more time, but if the file is repeatedly include, using autoload can greatly improve system performance.
As to whether to use autoload to free programmers, this is a matter of benevolence and wisdom. In my opinion, it is worthwhile to sacrifice this performance (in some cases, or even improve the performance) for more convenient development if conditions permit.
Include () and require () performance
For include (), the file should be read and evaluated every time when include () is executed;
For require (), the file is only processed once (in fact, the file content replaces the require () statement ).
This means that if the code containing one of these commands and the code that may be executed multiple times, the use of require () is more efficient.
On the other hand, if different files are read each time the code is executed, or there is a loop through a set of file stacks, use include (),
Because you can set a variable for the file name you want to include, this variable is used when the parameter is include.