开发者

Which is more efficient between __autoload() or include/require?

开发者 https://www.devze.com 2023-03-22 12:59 出处:网络
Whic开发者_如何学运维h has faster execution time: __autoload or include inside nested if statements?

Whic开发者_如何学运维h has faster execution time: __autoload or include inside nested if statements? Which is less error prone between the two?


spl_autoload_register (instead of __autoload) should be the more robust option: it allows you to write code much more flexible than what is possible with include statements, and you can keep everything centralized.

Regarding performance, IMHO it's not meaningful to discuss it unless you have measured that a significant percentage of your page load times are spent on loading includes. Even if this is true, you should use a PHP opcode cache to speed your application up instead of trying to extract marginal benefits from switching between include and autoloading.


Just use include/require and get readable, maintainable and standard code. This is much more important than the 0.001 second you might save.

By the way, are you really sure you need extreme performance of your PHP scripts? Is the including really the bottleneck?


Whether you're using include/require or __autoload/spl_autoload most time will be (in most cases anyway) spent on loading, parsing and compiling the included source code. Execution of those few lines of autolaoder function(s) will hardly matter in comparison.


Have a look at: http://framework.zend.com/manual/en/performance.classloading.html

require_once is actually an expensive operation (checking if the file has already be loaded). Autoloading will improve your performances.

As Jon suggested, you might want to try spl_autoload_register rather than __autoload (see his answer).

However, be aware that this is a small optimization, I'm just providing your question an answer.

Edit: at first, I wrote the opposite of what I suggest now, so I updated my answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号