如何自动加载扩展类?

发布于 2024-08-04 15:18:24 字数 298 浏览 2 评论 0原文

我打算使用 PHP 的自动加载功能来动态加载所需的类文件。现在,如果每个函数都有一个单独的文件,这可能会造成巨大的混乱,所以我希望并询问是否有一种方法可以让相关的类保留在 1 个类文件中并且仍然自动加载

function __autoload($class_name){
    include('classes/' . $class_name . '.class.php');
}

假设有一个类名 Animals 和然后另一个类名为狗。狗类扩展了动物类,现在如果我调用狗类但不调用动物类,动物类文件是否仍会加载?

I am planning to use PHP's autoload function to dynamicly load only class files that are needed. Now this could create a huge mess if every single function has a seperate file, So I am hoping and asking is there a way to have related classes remain in 1 class file and still be auto-loaded

function __autoload($class_name){
    include('classes/' . $class_name . '.class.php');
}

Let's say there is a class name animals and then another class named dogs. Dogs class extends animals class, now if I were to call the dogs class but NOT call the animals class, would the animals class file still be loaded?

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

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

发布评论

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

评论(4

盗琴音 2024-08-11 15:18:24

您是否考虑过对班级地点进行明确定义?有时将相关类分组很有意义。

这是一种经过验证的处理方法。

这段代码被放置在 auto_prepend_file 中(或首先包含),

class Import
{
    public static $_AutoLoad = array();
    public static $_Imported = array();

    public static function Load($sName)
    {
        if(! isset(self::$_AutoLoad[$sName]))
            throw new ImportError("Cannot import module with name '$sName'.");

        if(! isset(self::$_Imported[$sName]))
        {
            self::$_Imported[$sName] = True;
            require(self::$_AutoLoad[$sName]);
        }
    }

    public static function Push($sName, $sPath)
    {
        self::$_AutoLoad[$sName] = $sPath;
    }

    public static function Auto()
    {
        function __autoload($sClass)
        {
            Import::Load($sClass);
        }
    }
}

并在引导文件中定义您的类以及它们所在的文件。

//Define autoload items
Import::Push('Admin_Layout',        App::$Path . '/PHP/Admin_Layout.php');
Import::Push('Admin_Layout_Dialog', App::$Path . '/PHP/Admin_Layout.php');
Import::Push('FileClient',          App::$Path . '/PHP/FileClient.php');

最后,通过调用启用 AutoLoad 的

Import::Auto()

好处之一是您可以定义“模块”:

Import::Push('MyModule',          App::$Path . '/Module/MyModule/Init.php');

然后在需要时显式加载它们:

Import::Load('MyModule');

最好的部分之一是您可以在模块中添加额外的 Import::Push 行,这将在以下位置定义其所有类运行时。

Have you considered explicit definitions of your class locations? Sometimes it makes a lot of sense to group related classes.

Here is a proven way of handling it.

This code is placed in an auto_prepend_file (or included first)

class Import
{
    public static $_AutoLoad = array();
    public static $_Imported = array();

    public static function Load($sName)
    {
        if(! isset(self::$_AutoLoad[$sName]))
            throw new ImportError("Cannot import module with name '$sName'.");

        if(! isset(self::$_Imported[$sName]))
        {
            self::$_Imported[$sName] = True;
            require(self::$_AutoLoad[$sName]);
        }
    }

    public static function Push($sName, $sPath)
    {
        self::$_AutoLoad[$sName] = $sPath;
    }

    public static function Auto()
    {
        function __autoload($sClass)
        {
            Import::Load($sClass);
        }
    }
}

And in your bootstrap file, define your classes, and what file they are in.

//Define autoload items
Import::Push('Admin_Layout',        App::$Path . '/PHP/Admin_Layout.php');
Import::Push('Admin_Layout_Dialog', App::$Path . '/PHP/Admin_Layout.php');
Import::Push('FileClient',          App::$Path . '/PHP/FileClient.php');

And lastly, enable AutoLoad by calling

Import::Auto()

One of the nice things is that you can define "Modules":

Import::Push('MyModule',          App::$Path . '/Module/MyModule/Init.php');

And then load them explicitly when needed:

Import::Load('MyModule');

And one of the best parts is you can have additional Import::Push lines in the module, which will define all of its classes at runtime.

过气美图社 2024-08-11 15:18:24

如果我给狗打电话但是
不叫动物类,会吗
动物类文件仍然被加载吗?

是的。当您加载一个扩展另一个类的类时,PHP 必须加载基类,以便它知道它扩展了什么。

回复:每个文件存储多个类的想法:这不适用于您提供的自动加载功能。每个文件一个类确实是最佳实践,特别是对于自动加载的类。

如果一个文件中有多个类,那么您确实不应该尝试从该文件自动加载任何类。

if I were to call the dogs class but
NOT call the animals class, would the
animals class file still be loaded?

Yes. When you load an class that extends another class, PHP must load the base class so it knows what it's extending.

re: the idea of storing multiple classes per file: This will not work with the autoload function you provided. One class per file is really the best practice, especially for autoloaded classes.

If you have more than one class in a file, you really shouldn't attempt to autoload any classes from that file.

郁金香雨 2024-08-11 15:18:24

是的,除非您不包含/不需要类文件,否则它将加载。

您必须始终导入包含所需 PHP 代码的所有文件。 PHP 本身无法猜测您为类文件指定的名称。例如,与 Java 不同,PHP 对类没有任何文件命名要求。

解决这个问题的常见方法是将相关的类分组在一个文件中。创建一个“模块”。

Yes it will load unless you don't include/require the class file.

You have to always import any files that contain needed PHP code. PHP itself can't guess the name you gave to your class file. Unlike Java for instance, PHP doesn't have any file naming requirements for classes.

The common way so solve this problem is to group related classes in one single file. Creating a "module".

写给空气的情书 2024-08-11 15:18:24

有一个解决方法,我刚刚使用过它并且似乎有效:

假设我们有一个名为animals 的自动加载类,存储在animals.class.php 中。在同一个文件中,你可以有其他扩展动物的类

    class animals{
        static function load(){
            return true;
        }
    }


    class dogs extends animals{

    }

    class cats extends animals{

    }

现在...当你需要使用你的类 dogs 时,你需要 PHP 自动加载它的父类(这样做你确信它也会解析同一文件中的所有扩展类),因此您只需编写:

animals::load();
$fuffy = new dogs();

There's a workaround, I've just used it and it seems to work:

Let's assume we have an autoloaded class called animals stored in animals.class.php. In the same file you could have other classes that extends animals

    class animals{
        static function load(){
            return true;
        }
    }


    class dogs extends animals{

    }

    class cats extends animals{

    }

Now... when you need to use your class dogs you need PHP autoload it's parent class (and doing this you're sure it will parse also all the extendend classes inside the same file) so you just have to write:

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