Klasse mit Konstruktor-Property-Promotion (PHP 8.0+)
<?php
declare(strict_types=1);
final class Product
{
public function __construct(
public readonly string $name,
public readonly float $price,
private int $stock = 0,
) {}
public function increaseStock(int $amount): void
{
$this->stock += $amount;
}
public function isAvailable(): bool
{
return $this->stock > 0;
}
}
$p = new Product('T-Shirt', 24.90);
$p->increaseStock(50);
echo $p->name; // T-Shirt (public readonly)
// $p->name = 'x'; // Error: Cannot modify readonly property
Vererbung
class Animal
{
public function __construct(public string $name) {}
public function speak(): string
{
return "$this->name macht Geräusche";
}
}
class Dog extends Animal
{
public function speak(): string
{
return "$this->name bellt";
}
}
$d = new Dog('Bello');
echo $d->speak(); // Bello bellt
Interfaces
interface PaymentGateway
{
public function charge(int $amountInCents, string $currency): bool;
public function refund(string $transactionId): bool;
}
final class StripeGateway implements PaymentGateway
{
public function charge(int $amountInCents, string $currency): bool { /* ... */ }
public function refund(string $transactionId): bool { /* ... */ }
}
Abstrakte Klassen
Können nicht instanziiert werden. Erzwingen, dass Kinder bestimmte Methoden implementieren, teilen aber gemeinsamen Code:
abstract class Report
{
public function generate(): string
{
$data = $this->collect();
return $this->format($data);
}
abstract protected function collect(): array;
abstract protected function format(array $data): string;
}
Traits — horizontale Wiederverwendung
PHP hat keine Mehrfachvererbung. Traits sind der Ersatz: Methoden-Bausteine, die in mehrere Klassen eingemischt werden:
trait Loggable
{
public function log(string $msg): void
{
error_log('[' . static::class . '] ' . $msg);
}
}
class User { use Loggable; }
class Order { use Loggable; }
(new User)->log('Registriert'); // [User] Registriert
Statische Methoden & Properties
class Config
{
private static array $data = [];
public static function set(string $key, mixed $value): void
{
self::$data[$key] = $value;
}
public static function get(string $key): mixed
{
return self::$data[$key] ?? null;
}
}
Config::set('app.name', 'programmierung.net');
echo Config::get('app.name');
Readonly-Klassen (PHP 8.2+)
Alle Properties sind implizit readonly. Ideal für Value Objects und DTOs:
final readonly class Money
{
public function __construct(
public int $cents,
public string $currency,
) {}
public function plus(Money $other): Money
{
return new Money($this->cents + $other->cents, $this->currency);
}
}