
Xem Tắt
2.6.1.Mục đích¶
Để thực hiện một kiến trúc được ghép nối một cách lỏng lẻo để có được mã có thể kiểm tra, duy trì và mở rộng tốt hơn.
Bạn Đang Xem: Hướng dẫn php design patterns dependency injection – php thiết kế mẫu phụ thuộc tiêm
2.6.2.Cách sử dụng¶
Xem Thêm : Top 6 lịch khám theo yêu cầu bệnh viện đại học y hay nhất
DatabaseConfiguration
được tiêm và DatabaseConnection
sẽ nhận được tất cả những gì nó cần từ $config
.Nếu không có DI, cấu hình sẽ được tạo trực tiếp trong DatabaseConnection
, điều này không tốt cho việc thử nghiệm và mở rộng nó.
2.6.3.Ví dụ;
- Học thuyết2 ORM sử dụng tiêm phụ thuộc, ví dụ:Đối với cấu hình được tiêm vào một đối tượng
<?php declare(strict_types=1); namespace DesignPatternsStructuralDependencyInjection; class DatabaseConfiguration { public function __construct( private string $host, private int $port, private string $username, private string $password ) { } public function getHost(): string { return $this->host; } public function getPort(): int { return $this->port; } public function getUsername(): string { return $this->username; } public function getPassword(): string { return $this->password; } }
0.Đối với mục đích thử nghiệm, người ta có thể dễ dàng tạo một đối tượng giả của cấu hình và đưa vào đối tượng
<?php declare(strict_types=1); namespace DesignPatternsStructuralDependencyInjection; class DatabaseConfiguration { public function __construct( private string $host, private int $port, private string $username, private string $password ) { } public function getHost(): string { return $this->host; } public function getPort(): int { return $this->port; } public function getUsername(): string { return $this->username; } public function getPassword(): string { return $this->password; } }
0
- Nhiều khung đã có các thùng chứa cho DI tạo đối tượng thông qua mảng cấu hình và bơm chúng khi cần (tức là trong bộ điều khiển)
2.6.4.Sơ đồ uml
2.6.5.Mã số¶
Bạn cũng có thể tìm thấy mã này trên GitHub
Xem Thêm : Ngày 9 2 1930 diễn ra sự kiện nào dưới đây
DatabaseConfiguration.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php declare(strict_types=1); namespace DesignPatternsStructuralDependencyInjection; class DatabaseConfiguration { public function __construct( private string $host, private int $port, private string $username, private string $password ) { } public function getHost(): string { return $this->host; } public function getPort(): int { return $this->port; } public function getUsername(): string { return $this->username; } public function getPassword(): string { return $this->password; } }
DatabaseConnection.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php declare(strict_types=1); namespace DesignPatternsStructuralDependencyInjection; class DatabaseConnection { public function __construct(private DatabaseConfiguration $configuration) { } public function getDsn(): string { // this is just for the sake of demonstration, not a real DSN // notice that only the injected config is used here, so there is // a real separation of concerns here return sprintf( '%s:%[email protected]%s:%d', $this->configuration->getUsername(), $this->configuration->getPassword(), $this->configuration->getHost(), $this->configuration->getPort() ); } }
2.6.6.Bài kiểm tra¶
Tests/DependencyInjectionTest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php declare(strict_types=1); namespace DesignPatternsStructuralDependencyInjectionTests; use DesignPatternsStructuralDependencyInjectionDatabaseConfiguration; use DesignPatternsStructuralDependencyInjectionDatabaseConnection; use PHPUnitFrameworkTestCase; class DependencyInjectionTest extends TestCase { public function testDependencyInjection() { $config = new DatabaseConfiguration('localhost', 3306, 'domnikl', '1234'); $connection = new DatabaseConnection($config); $this->assertSame('domnikl:[email protected]:3306', $connection->getDsn()); } }
Nguồn: https://quatangtiny.com
Danh mục: Blog