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\models
but your namespace is FirstCap:
<?php
namespace MyApp\Models;
if you rename your folder to MyApp\src\Models
you 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 models
to Models
. You may have to rename from models
to somethingelse
(and commit) and then rename to Models
(and commit again)