'Autoload nested folder classes in custom namespace with composer
i have these directory and file structure.
i want to load my Wtf class in Models namespace.
in fact i want to add some providers to my project and want to locate them in own folder for classify them but call them with one namespace
its possible?
.
├── composer.json
├── index.php
├── src
│ ├── Controllers
│ │ └── GameController.php
│ ├── Game
│ │ └── Game.php
│ ├── Models
│ │ ├── Game.php
│ │ └── Wtf
│ │ └── Wtf.php
│ └── Player
│ └── Player.php
└── vendor
├── autoload.php
└── composer
├── autoload_classmap.php
├── autoload_namespaces.php
├── autoload_psr4.php
├── autoload_real.php
├── autoload_static.php
├── ClassLoader.php
└── LICENSE
Error
[500]: GET / - Uncaught Error: Class 'Rp76\Models\Wtf' not found in /home/reza/www/wtf/index.php:13
Stack trace:
#0 {main}
thrown in /home/reza/www/wtf/index.php on line 13
composer.json
{
"autoload": {
"psr-4": {
"Rp76\\": "src/"
}
}
}
index.php
<?php
use Rp76\Game\Game;
use Rp76\Controllers\GameController;
use Rp76\Player\Player;
require_once "vendor/autoload.php";
new Game();
new Player();
new GameController();
new \Rp76\Models\Game();
new \Rp76\Models\Wtf();
Wtf.php
<?php
namespace Rp76\Models;
class Wtf
{
public function __construct()
{
echo 'Wtf<br/>';
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|