延迟加载是一种将对象的初始化推迟到实际需要时才进行的设计模式。不是在应用程序启动时加载所有对象,而是按需加载对象,这可以显着提高性能和内存使用率。
让我们从一个简单的例子开始来理解核心概念:
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; } }
循环引用提出了特殊的挑战。这是一个全面的解决方案:
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; } }
以上是延迟加载和循环引用的详细内容。更多信息请关注PHP中文网其他相关文章!