質問

I have succesfully created an autoload function for a project I am working on.

I have several classes and 1 index where I instantiate all of the classes.

In some of the classes however I do need to require some other classes in order to let the project work.

My question being: How will it affect my project if I instead of requiring the few classes I need in the other classes require the autoload class?

役に立ちましたか?

解決

The whole idea of autoloading classes is that you don't need to worry about this anymore. To achieve this, once you added your individual autoloading function, it will be used.

Say you have the following setup:

index.php

spl_autoload_register(function ($class) {
    //something
});

$a = new Foo();

and the file where Foo is routed to

class Foo extends Bar {
    public function __construct() {
        $b = new Baz();
    }
}

Since the autoloading was already invoked when searching for Foo, the same rules will now apply to find Bar and Baz.

This means once you defined your personal autoloading rules, PHP will go with them (until you redefine). This doesn't only work for inheritence but regular class calls inside your classes as well.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top