box-o-sand/oldstuff/c&s20150725/inheritance.php

53 lines
802 B
PHP
Raw Normal View History

2015-07-25 18:03:01 +00:00
<?php
class Pet {
public $name;
public $isRunning = false;
2015-07-25 18:03:38 +00:00
private function __construct()
{}
public static function create($name)
{
$newPet = new static;
$newPet->name = $name;
return $newPet;
}
2015-07-25 18:03:01 +00:00
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;
}
}
2015-07-25 18:03:38 +00:00
$cat = Pet::create('Felix');
2015-07-25 18:03:01 +00:00
var_dump($cat);
$cat->run();
var_dump($cat);
2015-07-25 18:03:38 +00:00
$dog = Dog::create('Fido');
2015-07-25 18:03:01 +00:00
var_dump($dog);
var_dump($dog->chase($cat));
var_dump($dog);
var_dump($cat);