<?php class dog { static $Name; protected function getName() { return $this->Name; } } class Poodle extends dog { static function bark() { print "'Woof', says " . $this->getName(); } } dog::$Name = 'puppy'; echo dog::$Name;
<?php abstract class animal { function makenoise() { echo $this->noise; } } //the extends bit is called inheritance. both classes cat and dog //'inherit' makenoise from animal in this case class cat extends animal { public $noise = 'meow'; } $cat = new cat(); $cat->makenoise(); class dog extends animal { public $noise = 'woof'; } $dog = new dog(); $dog->makenoise();
<?php error_reporting(E_ALL); ini_set("display_errors", "On"); class dog { public $type = 'labrador'; function blah() { echo $this->type; } } $dog = new dog(); $dog->blah();
$animal->walk()->walk()->walk()->run()->run(); echo $animal->displayHealth(); echo "<br>"; class dog extends animal { public function __construct($name) { $this->name = $name; $this->health = 150; } public function pet() { $this->health += 5; } } $dog = new dog("dog"); $dog->walk()->walk()->walk()->run()->run()->pet(); echo $dog->displayHealth(); echo "<br>"; class dragon extends animal { public function __construct($name) { $this->health = 170; $this->name = $name; echo "<br>this is a dragon!"; } public function fly() { $this->health -= 10; return $this;
echo "<br>"; return $this; } } class dragon extends animal { public function __construct($name) { parent::__construct($name); $this->health = 170; } public function fly() { echo "Flying..... "; echo "<br>"; $this->health -= 10; return $this; } public function display_health() { echo "This is a dragon"; echo "<br>"; parent::display_health(); } } $animal1 = new animal("Rhino"); $animal1->walk()->walk()->walk()->run()->run()->display_health(); $animal2 = new dog("Lance the dog walker"); $animal2->walk()->walk()->walk()->run()->run()->pet()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->run()->walk()->walk()->walk()->display_health(); $animal3 = new dragon("Elysian"); $animal3->walk()->walk()->walk()->run()->run()->fly()->fly()->display_health();
<?php class animal { public $name = ""; public $age = 0; public function display() { echo "name " . $this->name . "<br>"; echo "age " . $this->age . "<br>"; } } class dog extends animal { } class cat extends animal { } $d = new dog(); $c = new cat(); $c->name = "meow"; $c->age = 67; $d->name = "bow"; $d->age = 45; $c->display(); $d->display();