PHP OOP:这段代码是如何工作的?

发布于 2024-10-03 04:24:12 字数 1750 浏览 0 评论 0原文

我对 PHP5/OOP 不太熟悉,无法真正理解以下代码的工作原理。在研究 __autoload() 函数时在 PHP.net 文档中找到了它。

我该怎么称呼这个? new MyClass1(); 或者 autoloader::model('myModel');

代码片段:

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,'model'));
        spl_autoload_register(array($this,'helper'));
        spl_autoload_register(array($this,'controller'));
        spl_autoload_register(array($this,'library'));
    }

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

    public function controller($class)
    {
        $class = preg_replace('/_controller$/ui','',$class);

        set_include_path(get_include_path().PATH_SEPARATOR.'/controller/');
        spl_autoload_extensions('.controller.php');
        spl_autoload($class);
    }

    public function model($class)
    {
        $class = preg_replace('/_model$/ui','',$class);

        set_include_path( dirname(__FILE__) . PATH_SEPARATOR.'/model/');
        spl_autoload_extensions('.model.php');
        spl_autoload($class);
    }

    public function helper($class)
    {
        $class = preg_replace('/_helper$/ui','',$class);

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

}

I'm kind of new to PHP5/OOP and can't really get my head around how the following code works. Found it on the PHP.net documentation when researching the __autoload()-function.

How shall I call this? new MyClass1(); Or autoloader::model('myModel'); ?

The snippet:

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,'model'));
        spl_autoload_register(array($this,'helper'));
        spl_autoload_register(array($this,'controller'));
        spl_autoload_register(array($this,'library'));
    }

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

    public function controller($class)
    {
        $class = preg_replace('/_controller$/ui','',$class);

        set_include_path(get_include_path().PATH_SEPARATOR.'/controller/');
        spl_autoload_extensions('.controller.php');
        spl_autoload($class);
    }

    public function model($class)
    {
        $class = preg_replace('/_model$/ui','',$class);

        set_include_path( dirname(__FILE__) . PATH_SEPARATOR.'/model/');
        spl_autoload_extensions('.model.php');
        spl_autoload($class);
    }

    public function helper($class)
    {
        $class = preg_replace('/_helper$/ui','',$class);

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2024-10-10 04:24:12
$autoloader = autoloader::init();

这将为您提供自动加载器实例。这个类想要成为一个单例。

所有 __autoloader 都是 php 在最后一刻尝试查找尚未包含的类。如果您有一个良好的命名/目录结构,您将无需担心包含/需要您自己的任何类...自动加载器将为您做这件事。

如果你想调用模型函数:

$autoloader->model('Model');

这将在调用上面的静态 init() 函数之后完成。

$autoloader = autoloader::init();

That will give you the autoloader instance. This class wants to be a singleton.

All __autoloader is is a last minute attempt by php to find a class that hasn't been included yet. If you have a good naming/directory structure you won't need to worry about including/requiring any of your own classes...the autoloader will do it for you.

if you wanted to call the model function:

$autoloader->model('Model');

This would be done after calling the static init() function from above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文