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?

WBOY
Release: 2023-03-01 20:48:02
Original
950 people have browsed it

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

Because the parent class uses this object, I can’t inject it in the subclass, but I need a different implementation.

I control the injected code:

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?

Writing like this won’t work, because the object is injected in the parent class. The place where it is injected in the parent class:

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?

Reply content:

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

Because the parent class uses this object, I can’t inject it in the subclass, but I need a different implementation.

I control the injected code:

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?

Writing like this won’t work, because the object is injected in the parent class. The place where it is injected in the parent class:

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?

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

Parent class:

<code class="php">public function update()
{
    $this->validate($this->getRequest(), ...);
}

protected function getRequest()
{
    return app(Request::class);
}</code>
Copy after login

Subcategories:

<code class="php">protected function getRequest()
{
    return app(RoleRequest::class);
}</code>
Copy after login

Method 2: Use traits.

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 depends on it at will.

Why is it designed like this?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!