trzczy
trzczy

Reputation: 1501

composer PSR-4 autoloader "class not found" error

I try to add DieEcho class that is not present in the project directory path. But get the error when I try to implement the class in index.php file.:

Fatal error: Uncaught Error: Class 'Trzczy\Debug\DieEcho' not found in /home/j/Projects/project84/public/index.php:8 Stack trace: #0 {main} thrown in /home/j/Projects/project84/public/index.php on line 8



The directory tree is this:

├── debug
│   └── php
│       └── DieEcho.php
└── project84
    ├── composer.json
    └── public
        └── index.php



I run composer dump-autoload so the entry in autoload_psr4.php appeared:

j@debian:~/Projects/project84$ cat ../../vendors/project84/composer/autoload_psr4.php 
<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname($vendorDir)).'/Projects/project84';

return array(
    //(...)
    'Trzczy\\Debug\\' => array($baseDir . '/../debug/php'), //the entry
    //(...)
);
j@debian:~/Projects/project84$ 



DieEcho.php class:

j@debian:~/Projects/project84$ cat ../debug/php/DieEcho.php 
<?php

namespace Trzczy\Debug;
class DieEcho
{
    //(...)
}
j@debian:~/Projects/project84$ 



I try implement the class in index.php in this way:

j@debian:~/Projects/project84$ cat public/index.php 
<?php
use Zend\Mvc\Application;
use Zend\Stdlib\ArrayUtils;
use Trzczy\Debug\DieEcho;

error_reporting(-1);
ini_set('display_errors', true);
new DieEcho;
    //(...)



update

composer.json:

j@debian:~/Projects/project84$ cat composer.json 
{
    "config": {
        "vendor-dir": "../../vendors/project84"
    },
    //(...)
    "autoload": {
        "psr-4": {
            "Application\\": "module/Application/src/",
            "Trzczy\\Debug\\": "../debug/php/"
        }
    },
    //(...)
}
j@debian:~/Projects/project84$ 

What did I wrong? This is in zend framework 3 project but I do not know if this information is important.

Upvotes: 0

Views: 764

Answers (1)

trzczy
trzczy

Reputation: 1501

The reason was that the including of autoload.php was placed after the class implementing. When I moved the implementing after the including autoload.php, it worked out. Thanks to @Magnus Eriksson comment.

Upvotes: 1

Related Questions