Home > PHP Framework > ThinkPHP > body text

Dependency injection in ThinkPHP6

王林
Release: 2023-06-20 08:12:09
Original
1401 people have browsed it

Dependency injection is a very important concept in modern PHP development. It can help developers better manage dependencies between classes and improve code scalability and reusability. In the PHP framework ThinkPHP6, dependency injection is also well supported.

In ThinkPHP6, we can perform dependency injection through annotations or configuration files. Let’s take a closer look at how to use these two methods.

First, let’s look at the annotation method. By using annotations in classes, ThinkPHP6 can automatically perform dependency injection. The steps for dependency injection using annotations are as follows:

  1. Create a class that requires dependency injection
namespace appcontroller;

use appserviceUserService;

class UserController 
{
    private $userService;
 
    public function __construct(UserService $userService) 
    {
        $this->userService = $userService;
    }
 
    public function index($userId) 
    {
        $user = $this->userService->getUserById($userId);
 
        return $user;
    }
}
Copy after login
  1. Use annotations in the constructor of the class that needs to be injected
use appserviceUserService;

class UserController 
{
    /**
     * @Inject
     * @var UserService
     */
    private $userService;
 
    public function __construct() {}
 
    public function index($userId) 
    {
        $user = $this->userService->getUserById($userId);
 
        return $user;
    }
}
Copy after login

In this example, we can achieve dependency by using the @Inject annotation on the constructor and specifying the name of the class that needs to be injected UserService injection.

Next, let’s take a look at the configuration file method. In this way, we can define the classes that need to be injected and their dependencies in the configuration file. The steps for dependency injection in the configuration file are as follows:

  1. Create classes that require dependency injection
namespace appcontroller;

class UserController 
{
    private $userService;
 
    public function __construct() {}
 
    public function index($userId) 
    {
        $user = $this->userService->getUserById($userId);
 
        return $user;
    }
}
Copy after login
  1. Configure in the configuration file

In app/config/service.php, add the following code:

return [
    'userService' => appserviceUserService::class,
];
Copy after login

In this example, we define a named userService Service, specify its corresponding class as appserviceUserService::class.

  1. Perform dependency injection
namespace appcontroller;

class UserController 
{
    private $userService;
 
    public function __construct()
    {
        $this->userService = app('userService');
    }
 
    public function index($userId) 
    {
        $user = $this->userService->getUserById($userId);
 
        return $user;
    }
}
Copy after login

In this example, we obtain userService from the container through the app('userService') method object and assign it to the $userService attribute to implement dependency injection.

The above are two ways to perform dependency injection in ThinkPHP6. They can both help us better manage the dependencies between classes, making the code more scalable and reusable.

The above is the detailed content of Dependency injection in ThinkPHP6. For more information, please follow other related articles on the PHP Chinese website!

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