Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP says class Carbon\Carbon is not found

ErrorClass "Carbon\Carbon" not found

Short answer

Carbon is a Composer package. PHP only finds Carbon\Carbon when the package is installed in vendor, your script loads vendor/autoload.php, and the class name is imported with use Carbon\Carbon or written fully qualified. Check these three in that order.

  • Applies to PHP 8.3, Composer 2, nesbot/carbon 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask Composer whether the package is installed in this project.

    composer show nesbot/carbon
  2. 2Match what you see, then jump to its fix

    1. 1composer show says the package is not found, or vendor/nesbot is missingThe package is not installed in this projectFix
    2. 2Carbon is in vendor but plain PHP scripts still cannot find the classThe Composer autoloader is not loadedFix
    3. 3The error names your own namespace, for example App\Http\Controllers\CarbonThe class name is not imported inside a namespaceFix

What does the error look like?

  • Class "Carbon\Carbon" not found
  • Class 'Carbon\Carbon' not found

Cause 1 of 3

The package is not installed in this project

1Check

Look at whether vendor/nesbot/carbon exists and whether composer.json requires it.

ls vendor/nesbot/
grep carbon composer.json

2Fix

Install it with Composer, which also writes it to composer.json.

composer require nesbot/carbon

3Verify

The version shows up.

composer show nesbot/carbon

Confirmed by github.com

Did this fix it?

Cause 2 of 3

The Composer autoloader is not loaded

1Check

Composer writes vendor/autoload.php for packages with autoload information. Your entry script has to require it once, before any class is used.

grep -n "vendor/autoload.php" index.php

2Fix

Require the autoloader at the top of the entry file. If you edited the autoload section of your own composer.json, regenerate it.

<?php
require __DIR__ . '/vendor/autoload.php';
// after changing autoload in composer.json:
// composer dump-autoload

3Verify

Run a one-line script.

php -r 'require "vendor/autoload.php"; echo Carbon\Carbon::now(), PHP_EOL;'

Confirmed by getcomposer.org

Did this fix it?

Cause 3 of 3

The class name is not imported inside a namespace

1Check

In a file with a namespace, a name without an import is looked up inside that namespace. The manual says a fully qualified name with a leading backslash ignores imports and the current namespace.

head -15 app/Http/Controllers/ReportController.php

2Fix

Import the class under the namespace line, or write it fully qualified.

namespace App\Http\Controllers;

use Carbon\Carbon;

$now = Carbon::now();
// or without an import:
$now = \Carbon\Carbon::now();

3Verify

Reload the page. The error is gone.

php -l app/Http/Controllers/ReportController.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Add every package with composer require, so composer.json and vendor stay in step.
  • Commit composer.lock and run composer install on the server after each deploy.
  • Import classes with use at the top of each file.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.