Since PHP5, the authorities have greatly enriched the object-oriented support, with one important change: the introduction of the __autoload () function, which no longer requires a stack of require or include in the header of the PHP script, as the words in the PHP function manual read: " It is invoked automatically when an attempt is made to use a class that has not been defined.
This mechanism has greatly reduced the burden on developers, as long as the architecture in the early days of the directory structure and naming norms, in the development process, the need for the code to use the class to require the corresponding files, reduce a lot of code.
However, it is also easy to run a program where a class file is include multiple times, such as the following four scripts:
#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 executing script1.php, include ' include2.php '; This line of code was executed once. This line of code was executed two times when the script2.php was executed.
Here is just a simple example, in the actual project, the number of include2.php may be include more. Does such a recurring include affect performance? For this I wrote a script to test.
#file: simpleclass.php
class Simpleclass {public
function __construct () {
echo get_time (). "\ r \ n";
}
}
#file:p hp_include.php for
($i = 0; $i < $loop; $i + +) {
include_once "simpleclass.php";
New Simpleclass ();
}
When the $loop value is 1 o'clock, the script takes about 0.00018906593322754 seconds, and when $loop is 1000, the scripts take about 0.076701879501343 seconds.
What if we use autoload to achieve it?
#file:p hp_autoload.php
function __autoload ($class _name) {
include_once $class _name. '. php ';
For
($i = 0; $i < $loop; $i + +) {
new Simpleclass ();
}
In this code, I define the __autoload function, almost the same script, when the $loop is 1 o'clock, takes 0.0002131462097168 seconds, and when $loop is 1000, time is only 1/7, 0.012391805648804 seconds for the preceding code.
But pay attention to look at the Simpleclass code, which output a line of strings, if you remove this line of output and then compare, what will be the result?
In the case of $loop with 1000, the former takes 0.057836055755615 seconds, and after using AutoLoad, just 0.00199294090271 seconds! The efficiency difference is nearly 30 times times!
As you can see from the above tests, AutoLoad consumes a little more time when a file is only being include once, but using autoload can greatly improve system performance if the file is repeatedly include.
As for whether to use the autoload to liberate the programmer, this is a benevolent. It seems to me that, given the conditions allowed, sacrificing this performance (in some cases, perhaps even improving performance) is more convenient for development and is worthwhile.