Composer Autoload does not find class

0

I registered the autoload on composer.json

"autoload": {
    "psr-4": {
        "Racioly\MeuPackage\": "src/"
    }
}

I created a folder called test in the project root, and I also created a file teste.php , this file contains the following code:

require_once __DIR__ . '/../vendor/autoload.php';

use Racioly\MeuPackage\Complex;

var_dump(new Complex(2110));

Within src I have class Complex :

namespace Complex;

class Complex {
...
}

But when I run the file through the terminal php teste.php I get the error:

  

PHP Fatal error: Uncaught Error: Class 'Racioly \ MyPackage \ Complex' not found in ... test.php: 7

That's exactly where I test the instance of Complex on var_dump()

    
asked by anonymous 04.04.2017 / 18:52

1 answer

0

Have you updated your autoload.php?

Try to run the following command inside your root folder (composer.json)

composer dump-autoload

I made some tests here and it ran normally, follow my sample code.

composer.json

{
    "name": "teste/teste",
    "authors": [
        {
            "name": "Jean Souza",
            "email": "[email protected]"
        }
    ],
    "require": {},
    "autoload": {
    "psr-4": {
        "Racioly\MeuPackage\": "src/"
    }
}
}

Complex.php (class is inside the src directory)

<?php
namespace Racioly\MeuPackage;

class Complex {

    public function __construct($x)
    {
      echo $x;
    }

}

index.php (in the project root)

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once __DIR__ . '/vendor/autoload.php';

use Racioly\MeuPackage\Complex;

var_dump(new Complex(213123));

Result:

213123object(Racioly\MeuPackage\Complex)#2 (0) { }
    
04.04.2017 / 20:06