Move the C&S thing into the attic
This commit is contained in:
52
oldstuff/c&s20150725/inheritance.php
Normal file
52
oldstuff/c&s20150725/inheritance.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
return $this->name . ' is running';
|
||||
}
|
||||
}
|
||||
|
||||
class Dog extends Pet {
|
||||
protected $currentChaseTarget;
|
||||
|
||||
public function chase(Pet $chaseTarget)
|
||||
{
|
||||
$this->isRunning = true;
|
||||
$this->currentChaseTarget = $chaseTarget;
|
||||
$this->currentChaseTarget->run();
|
||||
|
||||
return $this->name . ' is chasing ' . $chaseTarget->name;
|
||||
}
|
||||
}
|
||||
|
||||
$cat = Pet::create('Felix');
|
||||
|
||||
var_dump($cat);
|
||||
|
||||
$cat->run();
|
||||
|
||||
var_dump($cat);
|
||||
|
||||
$dog = Dog::create('Fido');
|
||||
|
||||
var_dump($dog);
|
||||
|
||||
var_dump($dog->chase($cat));
|
||||
|
||||
var_dump($dog);
|
||||
var_dump($cat);
|
Reference in New Issue
Block a user