PHP: class autoloading. Explain on the fingers with a simple example?

Good day! The PLO is not strong, but want to fix it.
Here in all sorts of framwork with MVC, I see the connection as classes do not require "class.php" but something like this:

namespace something\base;
use something\models\Login;


Climbed as expected in man php, but somehow it is difficult to perceive. So I decided to ask here. Could You for example two or three files show how to use namespace and use? For example there are a couple of classes and a file in which it is invoked using the aforementioned features. Framwork larger and features a leg break to disassemble it as they realize it. I just need a good and small example, and I'll take it. Thanks in advance!
August 19th 19 at 23:25
4 answers
August 19th 19 at 23:27
Solution
All your classes have in the folder app/ (for example)
All classes set namespace Fogersp\SuperProject
In the composer.write json in the autoload section

...
"autoload": {
 "psr4": {
 "Fogersp\\SuperProject\\": "app"
}
},
...


This means that classes with namespaces Fogersp\SuperProject autoloader will look in the folder app/
For example, the class Fogersp\SuperProject\DB\Entry must be placed in the file app/DB/Entry.php

Later in the project root run the command
composer dumpautoload
and the composer will create the autoloader.

Now at the entry point connected this loader and not worried about connection files
include '../vendor/autoload.php';

That's all =)
well, not the composer is the one, in fact) - devon_Ebert commented on August 19th 19 at 23:30
: So is easier. A couple of lines in the config file, one command in the terminal and no zaparok ) - eduardo_Crist commented on August 19th 19 at 23:33
easier. but to understand the mechanism is not interfered with) - devon_Ebert commented on August 19th 19 at 23:36
: Thank you, but first sort out without composer) to Use a third-party tool is always handy, but when I do not understand all the sooner. - jensen_Senger commented on August 19th 19 at 23:39
: : well, there will not argue - it is necessary to know - eduardo_Crist commented on August 19th 19 at 23:42
The only question is: isn't it generally accepted to call namespace sootvetstviya their directory? Maybe I just misunderstood something, but not appropriate at this moment:
For example, the class Fogersp\SuperProject\DB\Entry must be placed in the file app/DB/Entry.php

Or instead of app to register in composer.json 'SuperProject', or to 'SuperProject' add '/app'? I try, really. But it's not logical =) - Quinten_Hilpe commented on August 19th 19 at 23:45
: You can. But what is app? No sense not responsible.
Fogersp\SuperProject\DB\Entry here name of author, name of the project on the internal structure. Then it is better to make a folder of the SuperProject and mappit. Well, it's a matter of taste.
My example project consists of two subprojects, I prefer to record so
"psr-4": {
 "WofhTools\\": "private/application",
 "Dolphin\\": "private/dolphin"
 }
- eduardo_Crist commented on August 19th 19 at 23:48
: It is important to understand both in principle mapitse the namespace for the file structure. And the rest - at your discretion. - eduardo_Crist commented on August 19th 19 at 23:51
But what is app?

Yes, it's not meaning 'app', and the coincidence of names =)
And when You have two sub-namespace match the directory? - Quinten_Hilpe commented on August 19th 19 at 23:54
: they are always the same for psr-4 will not work otherwise. The point is what level they will match. "Dolphin\\": "private/dolphin" - here all namespaces starting with "Dolphin\\" will be searched in the folder "private/dolphin", and the rest of the namespace will be the same as file system - eduardo_Crist commented on August 19th 19 at 23:57
August 19th 19 at 23:29
Solution
Yes, just open and read the psr, there there are examples.
By the way, the autoloading of classes is irrelevant to the PLO.

in principle, all the basics are described here php.net/manual/ru/language.oop5.autoload.php

You'd better describe what exactly is not clear, then it would be possible to answer more specifically.
Thank you. Now I will read more. - devon_Ebert commented on August 19th 19 at 23:32
: well then, at the same time also www.php-fig.org/psr/psr-4
there are links to the examples. it is actually very simple, but difficult to answer General questions. - eduardo_Crist commented on August 19th 19 at 23:35
August 19th 19 at 23:31
you need to zaregat funko the autoloader, it will be when you access a class's namespace to load the class itself.
suggest to read about PSR-0 and PSR-4
August 19th 19 at 23:33
Both the autoloader load only those classes that are (advertised new) in this php file:

// Determine the location where the class files that will upload
define('DIR', __DIR__);
define('APP', DIR.'/app');

// Legacy version
function __autoload($class) {
 include APP. '/'. $class . '.php';
}

// Actual variant with a nameless function
spl_autoload_register(function ($class) {
 include APP. '/' . $class . '.php';
});

$obj1 = new MyClass1();
$obj1->displayVar();


In your example
namespace something\base;
use something\models\Login;
you define the namespace and tell it to use the word use
And why __autoload - outdated? - devon_Ebert commented on August 19th 19 at 23:36
: php.net/Autoload
While the function __autoload() can also be used for automatic loading of classes and interfaces that preference should be given to spl_autoload_register(), because it provides a much more flexible alternative, allowing you to register the required number of autoloaders, for example for the correct operation of third-party libraries. In this regard, the use of __autoload() is not recommended and may be deprecated in the future.
- eduardo_Crist commented on August 19th 19 at 23:39
yeah, already got it all figured out to read :) Thank you! :) - devon_Ebert commented on August 19th 19 at 23:42

Find more questions by tags PHP