Vererbung
Aus php bar
Vererbung bezeichnet den Vorgang, Methoden und Eingenschaften einer bestehenden Klasse (Parent) an eine neue Klasse (Child) zu vererben.
Beispiel
Der praktische Nutzen des Vererbens liegt darin, dass man Klassen und Objekte, die identische Methoden und Eigenschaften besitzen, von einer Elternklasse eben diese Methoden und Eigenschaften vererben lässt. Ein zweiter Nutzen besteht darin, dass man den Funktionsumfang einer bestehenden Klasse erweitern kann, ohne den ursprünglichen Umfang einbüssen zu müssen.
PHP 5
1 <?php 2 abstract class Vehicle { 3 protected $_type = 'Vehicle'; 4 protected $_velo = 100; 5 6 public function getType() { 7 return $this->_type; 8 } 9 10 public function setVelocity($velo) { 11 $this->_velo = $velo; 12 return $this; 13 } 14 15 public function getVelocity() { 16 return $this->_velo; 17 } 18 19 public function printInfo() { 20 echo 'Type: ' . $this->_type . "\n"; 21 echo 'Velocity: ' . $this->_velo . "\n"; 22 return $this; 23 } 24 } 25 26 class Car extends Vehicle { 27 protected $_type = 'Car'; 28 protected $_velo = '120'; 29 } 30 31 class Truck extends Car { 32 protected $_load = array(); 33 34 public function addLoad($obj) { 35 $this->_load[] = $obj; 36 return $this; 37 } 38 39 public function getLoad() { 40 return $this->_load; 41 } 42 } 43 44 class TGV extends Vehicle { 45 protected $_type = 'TGV Train'; 46 protected $_velo = '574'; 47 48 protected $_destination = 'Paris Est'; 49 50 public function getDestination() { 51 return $this->_destination; 52 } 53 54 public function setDestination($destination) { 55 $this->_destination = $destination; 56 return $this; 57 } 58 59 public function printInfo() { 60 echo 'Destination: ' . $this->_destination . "\n"; 61 return parent::printInfo(); 62 } 63 } 64 65 echo ' -- Car -- '."\n"; 66 67 $car = new Car(); 68 $car->printInfo(); 69 $car->setVelocity(200); 70 $car->printInfo(); 71 72 echo ' -- Truck -- '."\n"; 73 74 $truck = new Truck(); 75 $truck->addLoad('Crates'); 76 $truck->addLoad('Fruits'); 77 $truck->printInfo(); 78 print_r($truck->getLoad()); 79 80 echo ' -- TGV -- '."\n"; 81 82 $tgv = new TGV(); 83 $tgv->printInfo(); 84 $tgv->setVelocity(320)->setDestination('Frankfurt(M) Hbf'); 85 $tgv->printInfo(); 86 87 /* 88 89 -- Car -- 90 Type: Car 91 Velocity: 120 92 Type: Car 93 Velocity: 200 94 -- Truck -- 95 Type: Car 96 Velocity: 120 97 Array 98 ( 99 [0] => Crates 100 [1] => Fruits 101 ) 102 -- TGV -- 103 Destination: Paris Est 104 Type: TGV Train 105 Velocity: 574 106 Destination: Frankfurt(M) Hbf 107 Type: TGV Train 108 Velocity: 320 109 110 111 */ 112 113 ?>
PHP 4
1 <?php 2 class Fahrzeug 3 { 4 var $typ; 5 var $speed; 6 7 function getTyp() 8 { 9 return $this->typ; 10 } 11 12 function setSpeed($cur) 13 { 14 $this->speed = $cur; 15 } 16 } 17 18 class Auto extends Fahrzeug 19 { 20 var $personen; 21 22 function setPersonen($num) 23 { 24 $this->personen = $num; 25 } 26 } 27 28 class LKW extends Fahrzeug 29 { 30 var $ladung = array(); 31 32 function addLadung($value) { 33 $this->ladung[] = $value; 34 } 35 36 function getLadung() { 37 return $this->ladung; 38 } 39 } 40 41 $auto =& new Auto; 42 $auto->setPersonen(2); 43 $auto->setSpeed(50); 44 45 $lkw =& new LKW; 46 $lkw->addLadung("Stroh"); 47 echo "Folgende Ladungen liegen auf dem LKW"; 48 var_dump($lkw->getLadung()); 49 50 ?>

