Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
I am wondering what is the best practice for calling (initiating) custom classes that do not need to be included/started every time the script is ran. Currently I have a __autoload function for classes I miss, but I'd like to stick away from defaulting to it.
Is it best to manually include and call it everytime?
If it is used everytime, you might want to have an initialization routine (usually ran in a bootstrap file, index.php being the most common approach). Don't use globals though... it's bad practice.
I consider __autoload() the best solution. Anything else is going to either 1) load in classes that you don't yet need and may never need, or 2) make you do something more complex than $obj = new ClassName for instancing the class. Since these are costs, what's the benefit they're paying for? I'm not seeing it.
Even better than __autoload() is spl_autoload_register(). That way, different autoload functions in different libraries won't replace each other. You can add as many functions to load a file, and (as far as I know) then will be executed in the order they are defined.
function loadClass ($class) {
$file = "/classes/$class.php";
if (file_exists ($file)): include ($file); endif;
}
spl_autoload_register ('loadClass');
For autoloads to work best, your code should generally be one-file-per-class. If that style doesn't suit you, then by all means, include your heart out
If that is your actual code you are probably going to experience problems:
1. The double dollar sign will result in the label for the actual variable being returned NOT the value -- probably undesired
2. Global is not "normally" captilized...everytime I have seen it (never used it) it was normally all lowercase.
3. While placing the constant on the LHS is smart because it prevents anyone from ever making an assignment by accident as opposed to an evaluation (which is a difficult bug to track down as it's so easy to over look). Personally...I find that code reads much less clear or natural than if the variable is on the LHS and constant (if any) is RHS. After all you are logically testing to see if X equals 10 -- not so much does 10 equal X. Of course, this is just my opinion.
4. Why not use __autoload? Perhaps you would be better of implementing some kind of factory or service locator if you want to centralize object creation?
Hockey wrote:4. Why not use __autoload? Perhaps you would be better of implementing some kind of factory or service locator if you want to centralize object creation?
This is how Joomla! works, you use a custom function that replaces include ( jimport() ), this function tells the factory about the class and where to find it, later _autoload calls the factory with the name of the class, the factory class determines if there is already an instance, if so returns an instance to the existing object instead of re-creating it. This way you can call jimport() for a sub-package's dependencies and the factory class will handle the lazy loading http://en.wikipedia.org/wiki/Lazy_loading
Also instead of using globals why not call your factory class statically.