Le chargement différé est un modèle de conception qui diffère l'initialisation des objets jusqu'à ce qu'ils soient réellement nécessaires. Au lieu de charger tous les objets au démarrage de l'application, les objets sont chargés à la demande, ce qui peut améliorer considérablement les performances et l'utilisation de la mémoire.
Commençons par un exemple simple pour comprendre le concept de base :
class User { private ?Profile $profile = null; private int $id; public function __construct(int $id) { $this->id = $id; // Notice that Profile is not loaded here echo "User {$id} constructed without loading profile\n"; } public function getProfile(): Profile { // Load profile only when requested if ($this->profile === null) { echo "Loading profile for user {$this->id}\n"; $this->profile = new Profile($this->id); } return $this->profile; } } class Profile { private int $userId; private array $data; public function __construct(int $userId) { $this->userId = $userId; // Simulate database load $this->data = $this->loadProfileData($userId); } private function loadProfileData(int $userId): array { // Simulate expensive database operation sleep(1); // Represents database query time return ['name' => 'John Doe', 'email' => 'john@example.com']; } }
Le modèle Proxy offre une approche plus sophistiquée du chargement paresseux :
interface UserInterface { public function getName(): string; public function getEmail(): string; } class RealUser implements UserInterface { private string $name; private string $email; private array $expensiveData; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; $this->loadExpensiveData(); // Simulate heavy operation echo "Heavy data loaded for {$name}\n"; } private function loadExpensiveData(): void { sleep(1); // Simulate expensive operation $this->expensiveData = ['some' => 'data']; } public function getName(): string { return $this->name; } public function getEmail(): string { return $this->email; } } class LazyUserProxy implements UserInterface { private ?RealUser $realUser = null; private string $name; private string $email; public function __construct(string $name, string $email) { // Store only the minimal data needed $this->name = $name; $this->email = $email; echo "Proxy created for {$name} (lightweight)\n"; } private function initializeRealUser(): void { if ($this->realUser === null) { echo "Initializing real user object...\n"; $this->realUser = new RealUser($this->name, $this->email); } } public function getName(): string { // For simple properties, we can return directly without loading the real user return $this->name; } public function getEmail(): string { // For simple properties, we can return directly without loading the real user return $this->email; } }
Les références circulaires présentent un défi particulier. Voici une solution complète :
class User { private ?Profile $profile = null; private int $id; public function __construct(int $id) { $this->id = $id; // Notice that Profile is not loaded here echo "User {$id} constructed without loading profile\n"; } public function getProfile(): Profile { // Load profile only when requested if ($this->profile === null) { echo "Loading profile for user {$this->id}\n"; $this->profile = new Profile($this->id); } return $this->profile; } } class Profile { private int $userId; private array $data; public function __construct(int $userId) { $this->userId = $userId; // Simulate database load $this->data = $this->loadProfileData($userId); } private function loadProfileData(int $userId): array { // Simulate expensive database operation sleep(1); // Represents database query time return ['name' => 'John Doe', 'email' => 'john@example.com']; } }
interface UserInterface { public function getName(): string; public function getEmail(): string; } class RealUser implements UserInterface { private string $name; private string $email; private array $expensiveData; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; $this->loadExpensiveData(); // Simulate heavy operation echo "Heavy data loaded for {$name}\n"; } private function loadExpensiveData(): void { sleep(1); // Simulate expensive operation $this->expensiveData = ['some' => 'data']; } public function getName(): string { return $this->name; } public function getEmail(): string { return $this->email; } } class LazyUserProxy implements UserInterface { private ?RealUser $realUser = null; private string $name; private string $email; public function __construct(string $name, string $email) { // Store only the minimal data needed $this->name = $name; $this->email = $email; echo "Proxy created for {$name} (lightweight)\n"; } private function initializeRealUser(): void { if ($this->realUser === null) { echo "Initializing real user object...\n"; $this->realUser = new RealUser($this->name, $this->email); } } public function getName(): string { // For simple properties, we can return directly without loading the real user return $this->name; } public function getEmail(): string { // For simple properties, we can return directly without loading the real user return $this->email; } }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!