1
votes

I started a new PHP project and wanted to use PSR-4 to autoload from composer.

Project structure looks like this:

project

__src

____model

_______User.php

__test

_____UserTest.php

__vendor

__composer.json

This is my Composer.json:

{
    "autoload": {
        "psr-4": {
            "src\\model\\": "model/"
        }
    }
}

And this is my User.php

<?php
namespace model;

class User
{
    public function doSomething() {
        return true;
    }
}

and the UserTest.php

<?php
use model\User;

class UserTest extends PHPUnit_Framework_TestCase
{
    public function test(){
        $user = new User();
        $this->assertTrue($user->doSomething());
    }
}

When I run the unit test I get the following message:

Fatal error: Class 'model\User' not found.

1

1 Answers

3
votes

Your composer.json file is wrong. It should be

{
    "autoload": {
        "psr-4": {
            "model\\": "src/model/"
        }
     }
}