O
O
Optimus2017-08-15 11:16:39
PHP
Optimus, 2017-08-15 11:16:39

What is the advantage of class autoloader?

Simple code:

spl_autoload_register(function ($class) {
    include 'user/app/' . $class . '.php';
});
 
$obj  = new MyClass();
$obj->displayVar();

There are many classes in the user/app/ folder and the obvious advantage is that you don't have to include_once or require for each one. Those. we save a lot only on the time of interpreting files with classes, because even if we include them, but we don’t create an instance, then resources won’t be allocated to them anyway ... Is it
possible to get rid of the line and create an instance automatically at the moment ? Naturally in advance without creating "in reserve" on a copy of each class. $obj = new MyClass();$obj->displayVar();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
D3lphi, 2017-08-15
Pyan @marrk2

after all, even if we connect them, but we don’t create an instance, then they still won’t be allocated resources ...

Resources are spent on the class declaration, functions (they need to be stored somewhere). The code is passed through a lexical analyzer, tokens are created for each token, and a syntax tree is built from the tokens. So, even just including a file consumes resources.
No.
In 2017, it's common to use composer for autoloading, rather than writing your own bikes.

S
Stanislav B, 2017-08-15
@S_Borchev

no.

D
dmitriy, 2017-08-15
@dmitriylanets

And you can get rid of the line $obj = new MyClass(); and instantiate automatically?

can
function __constuct(MyClass $obj){
$this->obj = $obj;
}

container.thephppleague.com/2.x/auto-wiring

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question