自PHP5后,官方大大丰富了对面向对象的支持,其中有个重要改变:引入了__autoload()函数,从此不再需要在php脚本的header写一堆的require或include了,用PHP函数手册中的话说:”它会在试图使用尚未被定义的类时自动调用”。
这一机制大大减轻了开发人员的负担,只要在架构初期考虑好了目录结构和命名规范,在开发过程中,需要再为代码中要用到的类分别去require相应的文件,减少了大量代码。
但这样一来,也容易出现运行一个程序,某个类文件被include多次,例如有以下四个脚本:
#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
当执行script1.php时, include ‘include2.php’; 这行代码被执行了一次。而执行script2.php时,这行代码被执行了两次。
这里只是一个简单的例子,在实际的项目中,include2.php被include的次数可能更多。这样反复的include,是否会影响性能呢?为此我写了个脚本来测试。
#file:SimpleClass.php class SimpleClass { public function __construct() { echo get_time() . "\r\n"; } }
#file:php_include.php for($i = 0;$i <p>当$loop值为1时,脚本耗时约0.00018906593322754秒,当$loop为1000时,脚本耗时约0.076701879501343秒。</p> <p>如果我们用autoload实现呢?</p> <p class="wp_syntax"> </p><p class="code"></p><pre style="FONT-FAMILY: monospace" class="php php">#file:php_autoload.php function __autoload($class_name) { include_once $class_name . '.php'; } for($i = 0;$i <p>在这段代码中,我定义了__autoload函数,几乎一样的脚本,当$loop为1时,耗时0.0002131462097168秒,而当$loop为1000时,耗时仅为前面代码的1/7,0.012391805648804秒。</p> <p>但请注意看SimpleClass的代码,其中输出了一行字符串,如果去掉这行输出后再比较,会是什么样的结果呢?</p> <p>在$loop同为1000的情况下,前者耗时0.057836055755615秒,而使用了autoload后,仅仅0.00199294090271秒!效率相差近30倍!</p> <p>从上面的测试可以看出,当文件仅仅被include一次,autoload会消耗稍微多一点的时间,但如果在文件被反复include的情况下,使用autoload则能大大提高系统性能。</p> <p>至于是否要使用autoload来解放程序员,这就仁者见仁,智者见智了。在我看来,条件允许的前提下,牺牲这一点性能(某些情况下,甚至可能是提升性能)更为便捷的开发,是值得的。</p> <p style="width:100%;text-align:center;margin:10px 0"> <br> <br> </p><p style="width:100%;text-align:center;margin:10px 0"> </p><p class="clear"></p>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use
