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?