Creating pets with static method
This commit is contained in:
parent
06dbe9737b
commit
cd6ae7f261
@ -4,6 +4,16 @@ class Pet {
|
||||
public $name;
|
||||
public $isRunning = false;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
public static function create($name)
|
||||
{
|
||||
$newPet = new static;
|
||||
$newPet->name = $name;
|
||||
return $newPet;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->isRunning = true;
|
||||
@ -24,8 +34,7 @@ class Dog extends Pet {
|
||||
}
|
||||
}
|
||||
|
||||
$cat = new Pet();
|
||||
$cat->name = 'Felix';
|
||||
$cat = Pet::create('Felix');
|
||||
|
||||
var_dump($cat);
|
||||
|
||||
@ -33,8 +42,7 @@ $cat->run();
|
||||
|
||||
var_dump($cat);
|
||||
|
||||
$dog = new Dog();
|
||||
$dog->name = 'Fido';
|
||||
$dog = Dog::create('Fido');
|
||||
|
||||
var_dump($dog);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user