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

king_eke's avatar

Use Imported Classes From Parent Class In a PHP Trait Laravel

Hello, I'm trying to use my imported classes in a parent class in my trait without having to import them again

So like in my client controller i have these imported for use

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

//trait
use App\Http\Traits\ClientTraits\WebsiteTraits;

class ClientController extends Controller{
    use WebsiteTraits;
}

then in the trait


namespace App\Http\Traits\ClientTraits;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

trait WebsiteTraits {

}

so is it possible to not have to re-import it, or if i could put all my imported classes in a separate file then include it in the parent class and the trait. so if i changed something somewhere it'll reflect everywhere

0 likes
10 replies
bobbybouwmann's avatar

So includes are file based and not inheritance based in PHP. Also for your editor you have to be specific about your imports otherwise it won't understand it anymore.

So in your case you have to do the include in your controller and in your trait if you use the same class in both places. There is no way to do this with inheritance at this point in PHP.

Does this answer your question?

king_eke's avatar

@BOBBYBOUWMANN - if i understand you correctly,

so i've created a separate file lets call it file.php, this is all it has


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

then the controller and the triat

include base_path('app/Http/Traits/ClientTraits/file.php');

but got Class App\Http\Traits\ClientTraits\Request does not exist

king_eke's avatar

A lot of bad guys on this forum but very few answer me, are my questions too hard or not worth your time??

martinbean's avatar

@king_eke Well, not many people are going to want to spend their time helping you if you’re going to insult them.

king_eke's avatar

@MARTINBEAN - I didn't insult anyone, just stating what i've been experiencing here, everytime I post a question I hardly get replies.

martinbean's avatar
Level 80

@king_eke The solution is, you have to import class in each file. You can’t import classes via an included file.

bobbybouwmann's avatar

@king_eke Don't be so disrespectful dude! I gave the exact same answer as @martinbean but just in different wording!

So in your case you have to do the include in your controller and in your trait if you use the same class in both places. There is no way to do this with inheritance at this point in PHP.

This basically translates to "You have to import the class in each file"!

Next time don't be an ass!

Please or to participate in this conversation.