In laravel, I inject an object into the parent class, and then I want to inject different implementations of this object according to different subclasses. Is there any way?

Source: Internet
Author: User
I inject an object into the parent class, and then I want to inject different implementations of this object according to different subclasses. because the parent class uses this object, I cannot inject it in the subclass, but I need different implementations. I control the injection code: this cannot be written, because the object is injected into the parent class. the parent class...

I inject an object into the parent class, and then I want to inject different implementations of this object according to different subclasses.

Because the parent class uses this object, I cannot inject it in the subclass, but I need different implementations.

I control the injection code:

This cannot be written, because the object is injected into the parent class, where the injection in the parent class is:

Reply content:

I inject an object into the parent class, and then I want to inject different implementations of this object according to different subclasses.

Because the parent class uses this object, I cannot inject it in the subclass, but I need different implementations.

I control the injection code:

This cannot be written, because the object is injected into the parent class, where the injection in the parent class is:

Method 1: Do not inject instances directly into the action method of the parent class. the parent class obtains related instances from other methods. the subclass overrides this method and returns the self-injected instances. For example:

Parent class:

public function update(){    $this->validate($this->getRequest(), ...);}protected function getRequest(){    return app(Request::class);}

Subclass:

protected function getRequest(){    return app(RoleRequest::class);}

Method 2: Use trait.

Method 3: inject in _ construct. Similar to Method 1, the _ construct of the parent class does not depend on any parameters, and the _ construct of the subclass is randomly dependent.

Why is it designed like this?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.