return $this->firstProduct; } public function getSecondProduct() { if (!$this->secondProduct) { $this->secondProduct = new SecondProduct(); } return $this->secondProduct; } } class FirstProduct implements Product { public function getName() { return 'The first product'; } } class SecondProduct implements Product { public function getName() { return 'Second product'; } } $factory = new Factory(); print_r($factory->getFirstProduct()->getName()); // The first product print_r($factory->getSecondProduct()->getName()); // Second product print_r($factory->getFirstProduct()->getName()); // The first product
{ if (!$this->firstProduct) { $this->firstProduct = new FirstProduct(); } return $this->firstProduct; } public function getSecondProduct() { if (!$this->secondProduct) { $this->secondProduct = new SecondProduct(); } return $this->secondProduct; } } class FirstProduct implements Product { public function getName() { return 'The first Product'; } } class SecondProduct implements Product { public function getName() { return 'The second Product'; } } $factory = new Factory(); var_dump($factory->getFirstProduct()->getName()); var_dump($factory->getSecondProduct()->getName());