I have a script that uses autoload to load classes that arn't found. I don't deliberately include file though i can but i would like the autoload function to include the required files , because the script can be recursive that is if the class is already loaded i don't want to check the corresponding file is loaded and if class_exist on each recursion of the script. Please give me suggestion.
If you want to avoid __autoload
, you can use require_once
instead of include
.
The performance hit of using __autoload
may be considerable, especially because some opcode caches do not support it properly. However, given it's very handy, I'd say use it unless your opcode cache does not cache autoload includes.
If you have your autoloader set up to load your classes and aren't using require
(et al.) the autoloader will only be called if a class is referenced that doesn't exist. So there is never a need to check class_exists
in the autoloader (it won't be called if the class exists).
With regard to performance. If you are using large libraries, autoload can actually be faster as it only loads the files/classes that are required. Either way the speed hit is pretty negligible in my experience (always use an opcode cache, as others have mentioned).