0

I cannot seem to get Carbon work on my PHP project. I'm using Composer and installed Carbon with the following command:

Composer require nestbot/carbon;

I can see Carbon files in my vendor folder.

useCarbon\Carbon;

class Manager {

    public function x()
    {
        Carbon::now();
    }
}

I am getting this error

Fatal error: Uncaught Error: Class 'Carbon\Carbon' not found

What am I missing? Thanks!

1 Answers1

2

You should not only use a class but also require autoload file generated by composer:

<?php

require 'vendor/autoload.php';

use Carbon\Carbon;

class Manager {

    public function x()
    {
        Carbon::now();
    }
}
falinsky
  • 7,229
  • 3
  • 32
  • 56
  • note that the `require` for the autoloader should be in the bootstrap code for your php system, not at the top of a random class file. If you're using any of the common frameworks, it'll be there already. – Spudley Apr 05 '20 at 22:15
  • @Spudley definitely. – falinsky Apr 05 '20 at 22:16
  • Just using pure PHP here guys, the require fixed it. Is there another way to include it without it if im not using any frameworks? – Mantas Stanionis Apr 05 '20 at 22:30
  • 2
    @MantasStanionis the point is that you should `require` autoload file somewhere and once. If the main entry point of your project is this file - you can require it here. If not - require it in the main file. For example if you use a front-controller pattern - require it there. – falinsky Apr 05 '20 at 22:35