Autoload of Poser does not find class

Asked

Viewed 1,218 times

0

I registered the autoload in the composer.json

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

I created a folder called test at the root of the project, and 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 the class Complex:

namespace Complex;

class Complex {
...
}

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

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

which is exactly where I test the instance of Complex in the var_dump()

  • 1

    Your class complex this important the namespace Complex, however, in Composer you are setting that the folder src has the origin of the namespace Racioly\MeuPackage\ . In class Complex you need it important for this namespace. Read more about https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md

1 answer

0


You updated your autoload.php?

Try running the following command inside your root folder (Composer.json)

composer dump-autoload

I did some tests here and ran normally, follow my example 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 (at the root of the project)

<?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));

Upshot:

213123object(Racioly\MeuPackage\Complex)#2 (0) { }
  • I had put the wrong namespace. thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.