开发者

How will this autoload function?

开发者 https://www.devze.com 2023-04-13 05:49 出处:网络
Here is my auto loader class. I am learning PHP from this week. I have got the idea of autoloader from different articles and PHP in action Book, but I have no idea how it actually will function?

Here is my auto loader class. I am learning PHP from this week. I have got the idea of autoloader from different articles and PHP in action Book, but I have no idea how it actually will function?

class autoloader {
    public static $loader;

    public static function init()
    {
        if(self::$loader == NULL) {
            self::$loader = new self();
        }
        return self::$loader;
    }   

    public function __construct()
    {
        spl_autoload_register(array(this, 'library'));
        spl_autoload_register(array(this, 'controller'));
        spl_autoload_register(array(this, 'helper'));
        spl_autoload_register(array($this, 'model'));
    }

    public function library($class) 
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . '/lib');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }

    public function controller($class)
    {
        s开发者_如何学Pythonet_include_path(get_include_path() . PATH_SEPARATOR . '/controller');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }

    public function helper($class)
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . '/helper');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }

    public function model($class)
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . '/model');
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }
}

I would like to ask how exactly functions this autoloader? for example if i instanciate a class which is located under lobrary folder how this complete file will be processed?


First of all, when you instantiate a class, it's constructor function registers four functions which will handle class autoload.

$autoloader = new autoloader();

Each of them will look for files in its own, specific directory. There is no need to call autoloader::init() function, as all autoload functions already registered by instantiating the class.

Then, when you try instantiate a class, if PHP can't find it, before failing with error, it will run functions, registered by spl_autoload_register() in the order they were registered. (autoloader::library(), autoloader::controller(), autoloader::helper() and autoloader::model()), and, if after that class will be available, it will be instantiated.

Extending your example, here's some code snippet:

<?php
include 'autoloader.php'; // in this file we have our autoloader class

$autoloader = new autoloader();

$libClass = new someLib();

When we call new someLib(), PHP's autoload mechanism will look for file 'somelib.php' (!lowercase!) in all areas, that are mentioned in include_path directive. You see, each function adds its own location to this directive

set_include_path(get_include_path() . PATH_SEPARATOR . '/controller');

That means that folder named 'controller', which is located in your root directory, will be added to include_path, and then spl_autoload() will look for a file in it. So you have to create file /lib/somelib.php and have a class with name 'someLib'

Hope this helps

Additional reading: spl_autoload_register spl_autoload


@user991047 As a beginner i advice you to clear the concept of the class and OOP's based concepts. if you know already ignore advice.

The class you mentioned is mostly used in PHP based MVC(Model View-helper Controller) framework. framework is the set of php based classes for Model, Helper and Controller those helps to speed up the application development, security, code re usability and many more...

For the application those use PHP's MVC framework needs to set the path to the library of these classes so that user can develop own code without worrying about MVC library path. And that is the purpose this class

autoloader class you mentioned contains method

public static function init()

This method is states you can call this method directly with instantiating the autoloader class object. This method checks whether the object of class autoloader is instantiated? and if not then then it creates the object of same class through which class methods can be accessed.

Constructor are automatically called when object of class is created and so it will register all four method

public function library($class) 
public function controller($class) 
public function helper($class) 
public function model($class) 

These function helps to perform include_once() automatically for all MVC classes.

If you dont understand still first be clear about MVC pattern. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号