Signatur
__construct, __destruct, __get, __set, __isset, __unset, __call, __callStatic, __toString, __invoke, __clone, __serialize, __unserialize, __debugInfo
Beschreibung
Die häufigsten: __construct() (Konstruktor), __toString() (String-Konvertierung), __invoke() (Objekt als Funktion aufrufbar).
Rückgabewert
Typ
mixed
Beispiele
Callable Object
<?php
class Adder {
public function __invoke(int $a, int $b): int {
return $a + $b;
}
}
$add = new Adder();
echo $add(2, 3); // 5
__toString
<?php
class Money {
public function __construct(public int $cents, public string $currency) {}
public function __toString(): string {
return sprintf('%.2f %s', $this->cents / 100, $this->currency);
}
}
echo new Money(1990, 'EUR'); // 19.90 EUR