tags:

views:

418

answers:

4

From php.net...
In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

Now I am wanting to know, is it bad practice to solely use __autoload to load the appropriate classes on a dynamic site?

The way my site is settup is to include files into the index.php file, for example http://www.site.com/index.php?p=PAGE-I-WANT-TO-LOAD

So if I am on the forums section or the blogs section of my site, I want only appropriate classes and functions to be loaded, so I use autoload but I never include a file manually, should I be using __autoload as a last resort or is what I am doing fine even on a high traffic system?

+5  A: 

Bad? No. __autoload() is one of my favorite additions to PHP 5. It removes the responsibility (and annoyance) of manually having to include/require the class files necessary to your application. That being said, it's up to you as the developer to ensure that only the 'appropriate classes' are loaded. This is easily done with a structured naming scheme and directory structure. There are plenty examples online of how to properly use __autoload(), do a Google search and you'll find plenty of information.

Arms
Yes I use it already, I was asking if it is a performance hit to rely on it
jasondavis
@jason: You should probably make that clear in your question, because you don't mention anything about performance right now.
musicfreak
@musicfreak you are right but also if it was not good for performance then it would most likely be considered "bad practice"
jasondavis
If anything the performance should be better because you are deferring the file inclusion to the last possible moment, so with this you also make sure that there is always only one hit to the file being included.
Miha Hribar
+7  A: 

Autoload is a good way to load only what classes is needed.

In PHP 5 >= 5.1.2, most of the problems with the old __autoload() dissapeared, thanks to spl_autoload_register().

gnud
I just looked at the referenced spl_autoload_register() page, I don't really understand it though, is it needed or is using just the basic __autoload function ok?
jasondavis
If you want to have more than one autoloader you must use spl_autoload_register, if not, __autoload will do just fine.
Miha Hribar
The main selling point of spl_autoload_register() is that a class library can define it's own autoload function, without getting a runtime error because __autoload() is defined twice.
gnud
+2  A: 

The only danger to __autoload() is if you define a poor autoloading function. Generally, all you're going to get in terms of a performance hit is a few disk seeks as PHP looks for the right files that contain your classes. The upside is getting rid of all those annoying include() calls.

If you're worried about performance at this level, then you should already be using an opcode cache such as APC.

zombat
Yes I use APC, i'm not really to worried I just didn't want to continue using autoloader if it was bad to expect it to load files or use it as a backup, like if you forgot t laod a file, thanks for the info
jasondavis
+3  A: 

Now I am wanting to know, is it bad practice to solely use __autoload to load the appropriate classes on a dynamic site?

Not at all. You can rely on autoload, all you need to do is to devise a good naming convention and implement an efficient autoloader.

There is one major issue to consider. Autoloading and Zend Guard do not play well together, because Zend Guard tends to rename things, which will mean that the naming convention you decided to use will most likely not be the same. If you will be using Zend Guard (or any other obfuscator for that matter) you will most likely be forced to include all the files by hand.

Here is a quote from the Zend Guard user guide:

Autoloading classes will not work since the filename on the disk would not match the obfuscated class name.

Miha Hribar
I currently already use autoload, but this is a good point about it not working with Zend Guard which I don't use but still good to know about +1
jasondavis