Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

php - Autoload classes from different folders

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

# 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??)?

Thanks.

EDIT:

these are my classes file names in the controller folder:

class_controller_base.php
class_factory.php
etc

these are my classes file names in the model folder:

class_model_page.php
class_model_parent.php
etc

this is how I name my controller classes class usually (I use underscores and lowcaps),

class controller_base 
{
...
}

class controller_factory
{
...
}

this is how I name my model classes class usually (I use underscores and lowcaps),

class model_page 
    {
    ...
    }

    class model_parent
    {
    ...
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I see you are using controller_***** and model_***** as a class naming convention.

I read a fantastic article, which suggests an alternative naming convention using php's namespace.

I love this solution because it doesn't matter where I put my classes. The __autoload will find it no matter where it is in my file structure. It also allows me to call my classes whatever I want. I don't need a class naming convention for my code to work.

You can, for example, set up your folder structure like:

  • application/
    1. controllers/
      • Base.php
      • Factory.php
    2. models/
      • Page.php
      • Parent.php

Your classes can be set up like this:

<?php
namespace applicationcontrollers;
class Base {...}

and:

<?php
namespace applicationmodels;
class Page {...}

The autoloader could look like this (or see 'a note on autoloading' at the end):

function __autoload($className) {
    $file = $className . '.php';
    if(file_exists($file)) {
        require_once $file;
    }
}

Then... you can call classes in three ways:

$controller = new applicationcontrollersBase();
$model = new applicationmodelsPage();

or,

<?php
use applicationcontrollers as Controller;
use applicationmodels as Model;

...

$controller = new ControllerBase();
$model = new ModelPage();

or,

<?php
use applicationcontrollersBase;
use applicationmodelsPage;

...

$controller = new Base();
$model = new Page();

EDIT - a note on autoloading:

My main auto loader looks like this:

// autoload classes based on a 1:1 mapping from namespace to directory structure.
spl_autoload_register(function ($className) {

    # Usually I would just concatenate directly to $file variable below
    # this is just for easy viewing on Stack Overflow)
        $ds = DIRECTORY_SEPARATOR;
        $dir = __DIR__;

    // replace namespace separator with directory separator (prolly not required)
        $className = str_replace('\', $ds, $className);

    // get full name of file containing the required class
        $file = "{$dir}{$ds}{$className}.php";

    // get file if it is readable
        if (is_readable($file)) require_once $file;
});

This autoloader is a direct 1:1 mapping of class name to directory structure; the namespace is the directory path and the class name is the file name. So the class applicationcontrollersBase() defined above would load the file www/application/controllers/Base.php.

I put the autoloader into a file, bootstrap.php, which is in my root directory. This can either be included directly, or php.ini can be modified to auto_prepend_file so that it is included automatically on every request.

By using spl_autoload_register you can register multiple autoload functions to load the class files any which way you want. Ie, you could put some or all of your classes in one directory, or you could put some or all of your namespaced classes in the one file. Very flexible :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...