Class Firebase\JWT\JWT not found
Encountering a "Class not found" error immediately after installing a Composer package is a frustrating experience, but it usually points to a very specific configuration issue rather than a bug in the library itself. The error message Fatal error: Uncaught Error: Class 'Firebase\JWT\JWT' not found indicates that PHP cannot locate the namespace definition for the JWT class, even though the code executes without errors when run via the command line or within Composer scripts.
The root cause here is almost certainly a missing autoloader configuration. When you run composer require firebase/php-jwt, Composer downloads the package and its dependencies into your /var/www/html/vendor directory. However, simply having the files present is not enough; PHP needs to know how to map the namespace Firebase\JWT to the actual file path where the class lives. By default, modern PHP versions (5.5+) come with the Composer autoloader pre-loaded in many environments, but it is not included by default when you create a new script like token.php.
To resolve this, you must explicitly tell your web server which files to autoload before attempting to use any classes installed via Composer. You need to include the file located at vendor/autoload.php at the very top of your script. Without this line, PHP ignores the entire vendor directory until it encounters a class that exists in its standard library (like DateTime). Since Firebase\JWT\JWT is an external dependency, PHP never reaches the autoloader logic unless you explicitly require or include the file first.
Here is how the corrected token.php should look:
<?php
require __DIR__ . '/vendor/autoload.php';
use \Firebase\JWT\JWT;
$key = "example_key";
$token = array(
"iss" => "http://example.org",
"aud" => "http://example.com",
"iat" => 1356999524,
"nbf" => 1357000000
);
$jwt = JWT::encode($token, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
print_r($decoded);
The addition of require __DIR__ . '/vendor/autoload.php'; is the critical missing piece. This command ensures that Composer's PSR-4 autoloader initializes before your script attempts to resolve the Firebase\JWT namespace. Once this file is included, PHP scans the psr-4 map generated by Composer and correctly maps Firebase\JWT\JWT to the actual class definition inside the vendor/firebase/php-jwt/src/JWT.php file.
There are a few nuances worth considering depending on your deployment strategy. If you are developing locally using a framework like Laravel, Symfony, or even a plain PHP CLI application, the autoloader is often loaded automatically in the entry point (e.g., artisan, bin/console). However, standalone scripts run via a web server (like Apache or Nginx serving static files) do not have this context. In those scenarios, manually requiring the autoloader is mandatory.
Another potential pitfall involves the PHP version. Your environment is running PHP 7.0. While firebase/php-jwt supports this version, you should be aware that support for PHP 7.0 ended in December 2018. If you encounter compatibility issues with modern libraries or security patches, upgrading to a supported PHP version (8.x) is strongly recommended. The error you are seeing, however, is purely related to autoloading and is unrelated to the PHP version itself, provided it meets the library's minimum requirements.
If you prefer not to include the autoloader in every standalone script, you can configure your web server to autoload dependencies globally. This typically involves editing your composer.json file to set "autoload": true or ensuring that your application bootstrap file includes the autoloader at the very start of execution. However, for a simple test script like token.php, explicitly requiring the autoloader is the most direct and reliable solution.
For production applications built with frameworks, ensure that your service provider or bootstrap sequence loads Composer's autoload files early in the lifecycle. If you are building a custom framework or managing multiple scripts across different directories, always verify that each entry point has access to the vendor/autoload.php file relative to its location. This prevents scope-related issues where classes installed globally might not be visible if the script is executed outside the project root without proper path resolution.