R
R
Ruslan Karetsky2020-04-25 18:17:25
PHP
Ruslan Karetsky, 2020-04-25 18:17:25

Autoload obliges to specify namespace for all classes?

Today I got acquainted with the spl_autoload_register function . Its use implies two options:

  1. Call several times, specifying functions that include classes from different directories as an argument
  2. Call once, but based on the fact that each class has its own namespace


Actually, the questions are:
1) From the point of view of programming standards, how correct is it to specify a namespace for each class?
After all, you can easily get confused in this way, and the code begins to contain characters that it could have passed before. (Not counting the cases where namespace eliminates name conflicts)

2) Which of the two approaches for using spl_autoload_register is more correct? From the point of view of the same programming standards (OOP and PSR), as well as experience from practice, if possible.

PS. PSR-4 says:
A fully qualified class name must have the following structure: \<Namespace>(\<SubNamespace>)*\<ClassName>

It turns out that when using autoloading in a project, all classes must contain namespaces?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Northern Lights, 2020-04-25
@Buyyer

1) From the point of view of programming standards, how correct is it to specify a namespace for each class?
Yes, it is right. One file, one class, which must be in a specific namespace.
2) Which approach for using spl_autoload_register of the two listed is more correct?
Second. Because this is the standard. " Calling several times, specifying functions that include classes from different directories as an argument " is when you have a mess of code and you are trying to pull it all into modern realities.
Now the de facto composer is the standard, don't reinvent the wheel, don't bother with something that is practically not used in itself (spl_autoload_register) in projects.
1. Take a composer ( phar works for me under Windows if run via php), initialize it in your project:
php ../composer.phar init
2. Create a directory in the app called Test at the root - this will be the App\Test namespace, put the Test file there. php, in which declare the class Test with the namespace namespace App\Test;
3. In the composer.json file add:
{
.....
    "autoload": {
        "psr-4": {
            "App\\": ["app/"]
        }
    }
}

where app - will be the entire code of your application, those of your miracle classes.
Run php ../composer.phar dump-autoload
3. Connect only one line: require('./vendor/autoload.php');to the index file or to those files where classes with autoloading are needed.
4. You should get this:
5ea45cd48bcdf580929185.jpeg

F
FanatPHP, 2020-04-25
@FanatPHP

From the point of view of programming standards, how correct is it to specify a namespace for each class?

There are no other options.
After all, you can easily get confused in this way, and the code begins to contain characters that it could have passed before.

This is some kind of rubbish to which I do not even know what to answer. Namespaces serve to avoid confusion . I don't know about symbols at all.
Which approach for using spl_autoload_register of the two listed is more correct

Second

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question