All posts

Fix Missing Import in PHP

Resolve PHP class not found errors, covering namespace use statements, Composer autoloading, and PSR-4 mapping issues.

Missing Import Errors in PHP

PHP's Class 'X' not found and Call to undefined function errors mean the autoloader can't find your class or the function isn't imported.

Namespace Use Statements

// Error: Class 'Request' not found
// Fix: add use statement
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        // ...
    }
}

Composer Autoload

After adding a new class, regenerate the autoloader:

composer dump-autoload

PSR-4 Mapping

{
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

This maps App\Models\User to src/Models/User.php. The namespace must match the directory structure.

Missing Composer Package

composer require guzzlehttp/guzzle
composer require league/flysystem

PHP Built-in Functions

In namespaced code, built-in functions might need a leading backslash:

namespace App\Utils;

// If you have a local function with the same name,
// use \ to access the global one
$time = \time();
$encoded = \json_encode($data);

Common Laravel Imports

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

Bugsly captures Class not found errors in PHP with the full namespace and autoload configuration, making it straightforward to diagnose PSR-4 mapping issues.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free