Home  >  Article  >  PHP Framework  >  Dependency injection in ThinkPHP6

Dependency injection in ThinkPHP6

王林
王林Original
2023-06-20 08:12:091382browse

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;
    }
}
  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;
    }
}

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;
    }
}
  1. Configure in the configuration file

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

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

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;
    }
}

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!

Statement:
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
Previous article:Using Ajax in ThinkPHP6Next article:Using Ajax in ThinkPHP6