0

I just did a bit of Research about Optimizing my PHP Code. In my Code Im using this function to include all classes I require:

function __autoload($class_name){
   require_once "system/classes/".strtolower($class_name).".php";
}

Now I have read, that Magic functions or whatever __autoload is called should be avoided.

And also that require_once is really slow commpared to a simple include. I wondered if there is an alternativ to __autoload or whats the best way to modyfy this function. Is there a Speed Difference between require and include?

Thaks in advance for your suggestions!

EDIT: Does iit also make sens to Cache or precompile the classes?

j_s_stack
  • 667
  • 1
  • 5
  • 18
  • http://php.net/spl-autoload-register – Charlotte Dunois Jan 18 '15 at 22:57
  • There is no significant performance difference between include() and require(). If you're only using one autoload method, then the older `__autoload` is sufficient. `spl_autoload_register` is only a requirement to combine multiple autoloaders. – mario Jan 18 '15 at 22:58
  • possible duplicate of [php spl\_autoload\_register vs \_\_autoload?](http://stackoverflow.com/q/6894538) – mario Jan 18 '15 at 23:01
  • possible duplicate of [PHP include vs include\_once (speed)](http://stackoverflow.com/q/4326435) – mario Jan 18 '15 at 23:01
  • 1
    Re your edit.... if you're using a modern version of PHP with OpCache, it already does cache the compiled bytecode for your files – Mark Baker Jan 18 '15 at 23:03
  • Didnt know that @MarkBaker Thank! I dont Need `spl_autoload_register` – j_s_stack Jan 18 '15 at 23:04
  • A big benefit of spl_autoload_register is that it only loads the individual files for your classes if and when they are actually needed, that can help save your PHP memory as well – Mark Baker Jan 18 '15 at 23:16

0 Answers0