The class Test, in MyApp\src\models folder:
<?php
namespace MyApp\Models;
class Test
{
function __construct() {
echo "class Test running...";
}
}
The index page on project root:
<?php
require_once realpath("vendor/autoload.php");
use MyApp\Models\Test;
$test = new Test();
But when I run the command:
composer dump-autoload -o
The error message says:
MyApp/src\models\Test.php does not comply with psr-4 autoloading standard. Skipping.
But it generates vendor folder with autoload.php and other files anyway. And index.php works fine. So what I am missing here?
According to the spec, you need to have your namespace path in FirstCaps.
Your path is lowercase
MyApp\src\modelsbut your namespace is FirstCap:if you rename your folder to
MyApp\src\Modelsyou should be good to go.If you have already committed your code to git, and are on a windows PC be aware that since both NTFS and FAT filesystems are case insensitive, you may experience issues jsut renaming the folder from
modelstoModels. You may have to rename frommodelstosomethingelse(and commit) and then rename toModels(and commit again)