Heim >Backend-Entwicklung >PHP-Tutorial >Lazy Loading und Zirkelverweise
Lazy Loading ist ein Entwurfsmuster, das die Initialisierung von Objekten verzögert, bis sie tatsächlich benötigt werden. Anstatt alle Objekte beim Start der Anwendung zu laden, werden Objekte bei Bedarf geladen, was die Leistung und Speichernutzung erheblich verbessern kann.
Beginnen wir mit einem einfachen Beispiel, um das Kernkonzept zu verstehen:
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']; } }
Das Proxy-Muster bietet einen ausgefeilteren Ansatz für Lazy Loading:
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; } }
Zirkuläre Referenzen stellen eine besondere Herausforderung dar. Hier ist eine umfassende Lösung:
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; } }
Das obige ist der detaillierte Inhalt vonLazy Loading und Zirkelverweise. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!