PHP Objektorientierung
Objektorientierung
PHP wurde nicht von Anfang an als objektorientierte Programmiersprache entworfen 🡒 erst ab Version 4
mit Ähnlichkeiten zu Java
abstract class MeineKlasse extends MeineBasisKlasse
implements SchnittstelleA, SchnittstelleB {
protected $id;
public $value;
public static $count = 1;
const MAX = 1000;
public function __construct($id) {
$this->id = $id;
}
public function ausgabe() {
print "Meine Klasse " . $this->id . " " . $this->value;
}
public abstract function auswerten();
}
Vererbung
class Oberklasse {
function HelloWorld() {
echo "Hello World <br>";
}
}
class Unterklasse extends Oberklasse {
function HelloWorld() {
parent::HelloWorld();
echo "Hallo Welt <br>";
}
}
class UnterUnterklasse extends Unterklasse {
function HelloWorld() {
echo "Bonjour monde <br>";
}
}
Was ist die Ausgabe?
$object = new Unterklasse();
$object->HelloWorld();__construct, __get, __set
...
__toString
class TestClass {
public $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
public function __toString()
{
return $this->foo;
}
}
$class = new TestClass('Hello');
echo $class; // Ausgabe: Hello
⭐ Beispiel
class User {
public $name;
public $email;
public $password;
function setEmail($newEmail) {
if(filter_var($newEmail, FILTER_VALIDATE_EMAIL) !== false) {
$this->email = $newEmail;
return true;
}
return false;
}
}
$max = new User();
$max->name = "Max Mustermann";
$max->setEmail("max@muster.de");
$lisa = new User();
$lisa->name = "Lisa Meier";
$lisa->setEmail("lisa@meier.de");
echo "Max hat die E-Mail $max->email und Lisa die E-Mail $lisa->email";
class Oberklasse {
public static function HelloWorld() {
echo "O: Hello World <br>";
}
}
class Unterklasse extends Oberklasse {
public static function HelloWorld() {
parent::HelloWorld();
echo "U: Hallo Welt <br>";
//Oberklasse::HelloWorld();
//self::HelloWorld();
}
}
Oberklasse::HelloWorld();
Unterklasse::HelloWorld();
$obj = new Unterklasse();
$obj->HelloWorld();
$obj::HelloWorld();
public staticentweder bei beiden (wegenextends) oder keinem, ansonsten z.B.:
Fatal error: Cannot make static method Oberklasse::HelloWorld() non static in class Unterklasse
filter_var
Filtern einer Variablen mit einem angegebenen Filter
Übungsaufgaben
📋 Fabrik
Schreibe die im Klassendiagramm abgebildete Klasse.
Die Methode produktHerstellen enthält nur die Anweisung echo "Neues Produkt hergestellt!";
Überprüfe die Funktionsfähigkeit, indem du ein Objekt neueFabrik instanzierst und die drei verwendeten Methoden aufrufst.
📋 Fußballmannschaft
Implementiere das folgende Klassendiagramm in PHP
Danach sind ein "Mannschafts"-Objekt und die geforderten Spieler zu erzeugen. Zu guter Letzt sind die Spieler der Mannschaft hinzuzufügen. Alle gesuchten Methoden sollen einfach einen sinnvollen Text ausgeben. Lagere die Klassendefinitionen in ein separates PHP-File aus!
Mehrfachvererbung
über trait und use (seit 5.4)
trait call {
public function call() {
echo "ring ring<br>";
}
}
trait radio {
public function play() {
echo "lalala<br>";
}
}
class phone {
private $money = 10;
use call, radio {
call::call as private;
radio::play as play_radio;
}
public function call_prepaid() {
if($this->money > 0){
$this->call();
}
}
}
call::call(); // non-static method cannot be called statically ...
$p = new phone();
$p->call_prepaid();
$p->play_radio();
interface Person {
public function greet();
public function eat($food);
}
trait EatingTrait {
public function eat($food) {
$this->putInMouth($food);
}
private function putInMouth($food) {
// digest delicious food
}
}
class NicePerson implements Person {
use EatingTrait;
public function greet() {
echo 'Good day, good sir!';
}
}
class MeanPerson implements Person {
use EatingTrait;
public function greet() {
echo 'Your mother was a hamster!';
}
}