SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Autoload classes from different folders

    Hi,

    This is how I autoload all the classes in my `controllers` folder,

    PHP Code:
    # auto load controller classes
            
    function __autoload($class_name
            {
                
    $filename 'class_'.strtolower($class_name).'.php';
                
    $file AP_SITE.'controllers/'.$filename;
        
                if (
    file_exists($file) == false)
                {
                    return 
    false;
                }
                include (
    $file);
            } 
    But I have classes in `models` folder as well and I want to autoload them too - what should I do? Should I duplicate the autoload above and just change the path to `models/` (but isn't this repetitive??)?

    These are my classes file names in the controller folder:

    Code:
     
    class_controller_base.php
    class_controller_factory.php
    etc
    these are my classes file names in the model folder:

    Code:
    class_model_page.php
    class_model_parent.php
    etc
    this is how I name my controller classes class usually (I use underscores and lowcaps),

    PHP Code:
    class controller_base 
        
    {
        ...
        }
        
    class 
    controller_factory
        
    {
        ...
        } 
    this is how I name my model classes class usually (I use underscores and lowcaps),

    PHP Code:
    class model_page 
            
    {
            ...
            }
            
    class 
    model_parent
            
    {
            ...
            } 
    Thanks.

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you keep with the naming conventions and directory structure you've established, then you could simply take the class name, chop off the part from the first underscore on, add an 's', and that becomes the directory to look in.

    If you see that pattern changing in the future, it might be a good time to consider new naming conventions
    aaron-fisher.com - PHP articles and more

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aamonkey View Post
    If you keep with the naming conventions and directory structure you've established, then you could simply take the class name, chop off the part from the first underscore on, add an 's', and that becomes the directory to look in.

    If you see that pattern changing in the future, it might be a good time to consider new naming conventions
    Thanks. Here is my solution,

    PHP Code:
    class autoloader
        
    {
            private 
    $directory_name;
            
            public function 
    __construct($directory_name)
            {
                
    $this->directory_name $directory_name;
            }
        
            public function 
    autoload($class_name
            { 
                
    $file_name 'class_'.strtolower($class_name).'.php';
                
                
    $file AP_SITE.$this->directory_name.'/'.$file_name;

                if (
    file_exists($file) == false)
                {
                    return 
    false;
                }
                include (
    $file);
            }
        }
        
    $controllers_loader = new autoloader('controllers');
            
    $models_loader = new autoloader('models');
        
    spl_autoload_register(array($controllers_loader'autoload'));
            
    spl_autoload_register(array($models_loader'autoload')); 
    But I am not sure... what do you think?

    Thanks.


Bookmarks

Posting Permissions

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