在 PHP 中动态构建/加载类库

发布于 2024-07-29 04:07:05 字数 272 浏览 2 评论 0原文

我对 OO 编程相当陌生......

我正在构建最终将成为一个大型类库,供我的整个站点使用。 显然,在每个页面上加载整个库是浪费时间和精力......

所以我想做的是在每个页面上需要一个“config”php类文件,并且能够“调用”或“加载” “根据需要提供其他课程 - 从而根据我的需要扩展我的课程。

据我所知,由于范围问题,我无法使用配置类中的函数来简单地 include() 其他文件。

我有什么选择? 开发者通常如何处理这个问题,什么最稳定?

I am fairly new to OO programming...

I am building what will eventually turn out to be a large library of classes to be used throughout my site. Clearly, loading the entire library on every page is a waste of time and energy...

So what I would like to do is require a single "config" php class file on each page, and be able to "call" or "load" other classes as needed - thus extending my class according to my needs.

From what I know, I can't use a function in the config class to simply include() other files, because of scope issues.

What are my options? How do developers usually handle this problem, and what is the most stable?

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

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

发布评论

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

评论(2

许一世地老天荒 2024-08-05 04:07:05

您可以使用 __autoload() 或创建一个对象工厂来加载所需的文件当你需要它们时。

顺便说一句,如果您的库文件存在范围问题,您可能应该重构您的布局。 大多数库都是可以在任何范围内实例化的类集。

以下是一个非常基本的对象工厂示例。

class ObjectFactory {

    protected $LibraryPath;

    function __construct($LibraryPath) {
        $this->LibraryPath = $LibraryPath;
    }

    public function NewObject($Name, $Parameters = array()) {
        if (!class_exists($Name) && !$this->LoadClass($Name))
            die('Library File `'.$this->LibraryPath.'/'.$Name.'.Class.php` not found.');
        return new $Name($this, $Parameters);
    }

    public function LoadClass($Name) {
        $File = $this->LibraryPath.'/'.$Name.'.Class.php'; // Make your own structure.
        if (file_exists($File))
                return include($File);
        else    return false;
    }
}

// All of your library files should have access to the factory
class LibraryFile {

    protected $Factory;

    function __construct(&$Factory, $Parameters) {
        $this->Factory = $Factory;
    }
}

You can use __autoload() or create an Object Factory that will load the files required when you need them.

As an aside, if you're having scope issues with your library files, you should probably refactor your layout. Most libraries are sets of classes that can be instantiated in any scope.

The following is an example very basic object factory.

class ObjectFactory {

    protected $LibraryPath;

    function __construct($LibraryPath) {
        $this->LibraryPath = $LibraryPath;
    }

    public function NewObject($Name, $Parameters = array()) {
        if (!class_exists($Name) && !$this->LoadClass($Name))
            die('Library File `'.$this->LibraryPath.'/'.$Name.'.Class.php` not found.');
        return new $Name($this, $Parameters);
    }

    public function LoadClass($Name) {
        $File = $this->LibraryPath.'/'.$Name.'.Class.php'; // Make your own structure.
        if (file_exists($File))
                return include($File);
        else    return false;
    }
}

// All of your library files should have access to the factory
class LibraryFile {

    protected $Factory;

    function __construct(&$Factory, $Parameters) {
        $this->Factory = $Factory;
    }
}

听起来你想要 autoloadspl_autoload_register 如果您使用来自第 3 方库的类。

Sound like you want autoload and spl_autoload_register if you are using classes from 3rd party libraries.

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