<?php
class Person
{
private $name;
private $surname;
private function __construct($name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
public static function born($name, $surname)
{
return new self(
$name,
$surname
);
}
public function changeName($newName)
{
$this->name = $name;
}
}
$newPerson = Person::born('event', '15');
$newPerson->changeName('efik');
Oto kodzik.
Mówię o zaletach. Klasy Person nie wywoła się w żaden sposób jak tylko poprzez znaną metodę born. Nikt nigdzie w kodzie nie stworzy tego poprzez new Person().
Chciałbym poznać opinię waszą na temat tego rozwiązania.
Temat jest też poruszony na PHPersach