Laravel Error Message Uncaught ReflectionException: Class 0 does not exist

Posted on

This is an article written to point out the error specified in the title of this article. It is an error message generated with the message of “Uncaught ReflectionException: Class 0 does not exist”. The error message appears upon executing the following command which involving artisan located inside the Laravel root folder directory as shown below :

root@hostname:/var/www/html/laravel-project# php artisan app:name Laravel
PHP Notice:  Use of undefined constant Laravel - assumed 'Laravel' in /var/www/html/laravel-project/bootstrap/app.php on line 31
PHP Notice:  Use of undefined constant Laravel - assumed 'Laravel' in /var/www/html/laravel-project/bootstrap/app.php on line 36
PHP Notice:  Use of undefined constant Laravel - assumed 'Laravel' in /var/www/html/laravel-project/bootstrap/app.php on line 41
PHP Fatal error:  Uncaught ReflectionException: Class 0 does not exist in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php:749
Stack trace:
#0 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php(749): ReflectionClass->__construct('0')
#1 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php(231): Illuminate\Container\Container->build(0, Array)
#2 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php(746): Illuminate\Container\Container->Illuminate\Container\{closure}(Object(Illuminate\Foundation\Application), Array)
#3 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php(644): Illuminate\Container\Container->build(Object(Closure), Array)
#4 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(709): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
#5 /var/www/html/laravel-project in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 749
root@hostname:/var/www/html/laravel-project#

The above error happened after executing the following command :

root@hostname:/var/www/html/laravel-project$ php artisan app:name Laravel-Testing
Application namespace set!
The compiled class file has been removed.
root@hostname:/var/www/html/laravel-project$

Apparently, the above command change the whole namespace of the files which is actually the main source code of the application including controllers, providers, models and also every files which is using them. One of them is actually exist in the vendor folder with the file named Container.php. And if the line of code where the error happened as specified in the error message which is in the line 749, in the context of the article it is Laravel 5.3 which is used to build the web-based application, it shows the following snippet code :

       $reflector = new ReflectionClass($concrete);

The notice given as a PHP Notice about an undefined constant ‘Laravel’ in line 31 located in bootstrap/app.php :

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    Laravel-Testing\Http\Kernel::class
    App\Http\Kernel::class
);

Another notice given as PHP Notice concerning undefined constant ‘Laravel’ in line 36 located in bootstrap/app.php :

$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
Laravel-Testing\Console\Kernel::class
);

The third notice as the form of PHP Notice concerning undefined constant ‘Laravel’ in line 41 located in bootstrap/app.php :

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    Laravel-Testing\Exceptions\Handler::class
    App\Exceptions\Handler::class
);

As changing the name of the application namespace via artisan command has also failed since the error as shown above prevent it from doing that, the method for solving the problem can be achieved through editing file bootstrap/app.php manually. So, change the ‘Laravel-Testing\’ into ‘App\’ to a default and original workspace. Don’t forget to update the namespace in all of the controllers, providers, models used in the application.

$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);

Another notice given as PHP Notice concerning undefined constant ‘Laravel’ in line 36 located in bootstrap/app.php :

$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);

The third notice as the form of PHP Notice concerning undefined constant ‘Laravel’ in line 41 located in bootstrap/app.php :

$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);

Last but not least, just execute ‘composer dumpautoload -o’ before re-execute the web-based application. For another solution which is more pleasant and simple, please go ahead and post the solution. That if the error itself is actually relates to the namespace problem.

One thought on “Laravel Error Message Uncaught ReflectionException: Class 0 does not exist

Leave a Reply