Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    19
    Thanks
    1
    Thanked 2 Times in 2 Posts

    PHP Class Loader

    Here's a handy little function I created to load classes. I'll provide the actual function and then an example of how to use it with a class. You're welcome to use this, but please leave the copyright intact/give credit where it's due!

    PHP Code:
    /*
    load - Loads the selected class file, initializes the class.

    Parameters
        $class = PHP class file
                NOTE: Do NOT include .php
                ALSO NOTE: The class name should be the file name.
        $mode = The mode to run; this allows the same class file to be used for multiple pages if needed.
            (Optional) Default: main
                        NOTE: all classes must have a main mode to default to!

    Return - Returns the class in variable $class
    */
    function load($class$mode 'main')
    {
        
    $class_file ROOT_PATH 'path/to/classes/
        ' 
    $class PHP_EXT;
        
    //! File does not exist; we end the script with a nice little error message
        
    if(!file_exists($class_file))
        {
            return 
    false;
        }
        else 
    //! File exists, so we move on.
        
    {
            include(
    $class_file); //! Include the file
            
            //! See if class exists in the included class file.
            //! Because it should be the same as the file name, we can just use the class file name provided.
            
    if(!class_exists($class)) //! If it does not exist, show a nice little error message and abort.
            
    {
                return 
    false;
            }
            else 
    //! Class exists, make an object using a variable variable. :D lol
            
    {
                
    $class = new $class;
                
    $class->main($mode);
                return 
    $class//! Return the class object
            
    }
        }

    Here's an example of a class to use it with:
    PHP Code:
    class test
    {
        
    /*
        main - Initialize the class for the current page
        
        Parameters
            $mode = The mode to use; (Optional) Default: main
        */
        
    function main($mode 'main')
        {
            
    //! All methods should have a main $mode option available, as that is
            //!        the default value given if no mode is specified.
            
    switch($mode)
            {
                default:
                case 
    'main':
                              
    //default content
                
    break;
                
                
    //! All classes should have a silent mode
                
    case 'silent':
                    
    //! Nothing here...
                                    // this way, you can call the class but not output anything; so you can access methods and class variables in other parts of the site.
                
    break;
            }
        }

    And here's how it's used in your index file (note: with this method, only an index file is needed, other pages are accessed with ?i=<page>).
    PHP Code:
    //! Let's get it started in here...
    //! $i is the module to load
    $i $_GET['i'];
    //! $mode is the mode to run
    $mode $_GET['mode']

    switch(
    $i)
    {
        case 
    'index':
        case 
    'main':
            case 
    'site':
            
    $main load('test'$mode);
        break;
        
        default:
            
    $main load($i$mode);
        break;
    }

    //! Assuming you have put all the stuff you need into the class mode that you are using, nothing else needs to go below this line.
    //! Cheers! 
    Last edited by imkingdavid; 05-09-2010 at 06:41 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    The return is totally pointless:

    Code:
                exit;
                return;
    The script has been killed by the exit. The return can never be called. By the way, if I remember correctly, (never used it myself, so may be incorrect), but could you not just the autoload function to achieve this functionality with far less code?

    http://uk2.php.net/manual/en/function.spl-autoload.php
    Last edited by MattF; 05-09-2010 at 03:35 PM.

  • #3
    New Coder
    Join Date
    Nov 2008
    Posts
    19
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by MattF View Post
    The return is totally pointless:

    Code:
                exit;
                return;
    The script has been killed by the exit. The return can never be called. By the way, if I remember correctly, (never used it myself, so may be incorrect), but could you not just the autoload function to achieve this functionality with far less code?

    http://uk2.php.net/manual/en/function.spl-autoload.php
    As for the exit-return bit, yeah I had noticed that but never thought to take it out. I'll just change it to return false instead of exit.

    As for spl_autoload, I had not seen it before. I'll look into it, but I like having my own method so I know exactly how it works and everything.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by imkingdavid View Post
    but I like having my own method so I know exactly how it works and everything.
    Can't argue with that. I tend to be in that camp too.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •