Namespace/ folder convention.
Classes should be stored in folders according to their namespaces.
In general, you will create an src/ directory in your root folder, sitting at the same level as vendor/, and add your projects there. Below is an example of the folder structure:
.
+-- src
|
+-- Book
| +-- History
| | +-- UnitedStates.php - namespace Book\History;
+-- Vehicle
| +-- Air
| | +-- Wings
| | | +-- Airplane.php - namespace Vehicle\Air\Wings;
| +-- Road
| | +-- Car.php - namespace Vehicle\Road;
+-- tests
+-- test.php
+-- vendor
Difference between psr-0 and psr-4
psr-0
It is deprecated. Looking at vendor/composer/autoload_namespaces.php
file you can see the namespaces and the directories that they are mapped to.
composer.json
"autoload": {
"psr-0": {
"Book\\": "src/",
"Vehicle\\": "src/"
}
}
- Looking for Book\History\UnitedStates in src/Book/History/UnitedStates.php
- Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php
psr-4
Looking at vendor/composer/autoload_psr4.php
file you can see the namespaces and the directories that they are mapped to.
composer.json
"autoload": {
"psr-4": {
"Book\\": "src/",
"Vehicle\\": "src/"
}
}
- Looking for Book\History\UnitedStates in src/History/UnitedStates.php
- Looking for Vehicle\Air\Wings\Airplane in src/Air/Wings/Airplane.php
composer.json
"autoload": {
"psr-4": {
"Book\\": "src/Book/",
"Vehicle\\": "src/Vehicle/"
}
}
- Looking for Book\History\UnitedStates src/Book/History/UnitedStates.php
- Looking for Vehicle\Air\Wings\Airplane in src/Vehicle/Air/Wings/Airplane.php