Calling Classes. Best Practice?

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.

Moderator: General Moderators

Post Reply
Chacha102
Forum Newbie
Posts: 1
Joined: Tue Jul 29, 2008 4:02 pm

Calling Classes. Best Practice?

Post by Chacha102 »

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?

Code: Select all

require_once("folder/file.php");
    $var = new class;
Create a function?

Code: Select all

function startClass($class)
        {
            Global $$class;
            if(TRUE != is_object($$class))
            {
                require_once("folder/" . $class .".php");
                $this->$class = new $class;
            }
            else
            {
                $this->$class = $$class;
            }
        }
       
Create a 'lib' function that can be included into every page (I have a 'lib' class that has a great deal of functions) and will return the class.

Code: Select all

function startClass($class)
        {
            Global $$class;
            if(TRUE != is_object($$class))
            {
                require("folder/" . $class . ".php");
                return new $class;
            }
            else
            {
                return $$class;
            }
        }
 
Or default to the __autoload() function? Or is there a better way? Or am I doing something REALLY wrong?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Calling Classes. Best Practice?

Post by Eran »

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.
User avatar
chaos
Forum Newbie
Posts: 22
Joined: Thu May 15, 2008 9:20 am
Location: New Jersey

Re: Calling Classes. Best Practice?

Post by chaos »

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.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Calling Classes. Best Practice?

Post by allspiritseve »

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.

Example:

Code: Select all

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 :D
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Calling Classes. Best Practice?

Post by alex.barylski »

Code: Select all

 
Global $$class;
            if(TRUE != is_object($$class))
 
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. :P

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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Calling Classes. Best Practice?

Post by josh »

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.
Post Reply