F
F
fadeevden2018-04-16 07:03:38
PHP
fadeevden, 2018-04-16 07:03:38

Why autoload classes?

Why autoload classes? Just for convenience or does it save some other resources?
// There is a variant with a function

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

There is an option through Composer
Or you can simply do 100 reqire_once () at the beginning of the file, indicating the class path. What is the difference in these methods?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Tallmange, 2018-04-16
@fadeevden

It provides loading on demand , not these yours.
It's possible, but what for? Always drag everything everywhere. In addition, as long as you have one file in which the includes are made, it’s still all right. But there are projects larger than your site, where there are hundreds and thousands of files . It's very convenient to add another require_once() to a thousand files, right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question