Karina
Karina

Reputation: 988

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

Which has faster execution time: __autoload or include inside nested if statements? Which is less error prone between the two?

Upvotes: 0

Views: 467

Answers (4)

Matthieu Napoli
Matthieu Napoli

Reputation: 49553

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.

Upvotes: 0

Mchl
Mchl

Reputation: 62387

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.

Upvotes: 0

Jon
Jon

Reputation: 437386

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.

Upvotes: 3

orlp
orlp

Reputation: 117691

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?

Upvotes: 2

Related Questions