Creating pets with static method

This commit is contained in:
Dan Buch 2015-07-25 14:03:38 -04:00
parent 06dbe9737b
commit cd6ae7f261

View File

@ -4,6 +4,16 @@ class Pet {
public $name; public $name;
public $isRunning = false; public $isRunning = false;
private function __construct()
{}
public static function create($name)
{
$newPet = new static;
$newPet->name = $name;
return $newPet;
}
public function run() public function run()
{ {
$this->isRunning = true; $this->isRunning = true;
@ -24,8 +34,7 @@ class Dog extends Pet {
} }
} }
$cat = new Pet(); $cat = Pet::create('Felix');
$cat->name = 'Felix';
var_dump($cat); var_dump($cat);
@ -33,8 +42,7 @@ $cat->run();
var_dump($cat); var_dump($cat);
$dog = new Dog(); $dog = Dog::create('Fido');
$dog->name = 'Fido';
var_dump($dog); var_dump($dog);