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
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.
@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"!