How to autoload extended classes?

送分小仙女□ 提交于 2019-12-03 13:59:02

问题


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?


回答1:


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.




回答2:


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.




回答3:


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".




回答4:


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();


来源:https://stackoverflow.com/questions/1441686/how-to-autoload-extended-classes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!