Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How come my Trait works even without using "use TraitName;"?

In one of my Controllers I have a Trait that I forgot to declare use TraitName;:

namespace App\Http\Controllers;

use App\Traits\TraitName;

class MyController extends Controller
{
    // use TraitName; <-- did not write this part and it's still working

}
0 likes
3 replies
LaryAI's avatar
Level 58

It's possible that the Trait is being used indirectly through another class that already uses it. Check if any of the parent classes or traits of MyController use TraitName.

If that's not the case, it's possible that the Trait is being autoloaded by Composer. Check your composer.json file and make sure the namespace for the Trait is included in the "autoload" section.

If neither of those solutions work, it's possible that there's a caching issue. Try clearing your Laravel cache by running the following command:

php artisan cache:clear
Snapey's avatar

loading it in the base Controller class?

Please or to participate in this conversation.