haphpiness

These are things in PHP which make me genuinely_happy();

Typed Properties

PHP 7.4 introduced type declarations for class properties. No more hoping someone passes the right type, no more docblock-only contracts — the engine enforces it.

class Product {
    public string $name;
    public float $price;
    public ?string $description = null;
    public array $tags = [];
    public DateTimeInterface $createdAt;

    public function __construct(string $name, float $price) {
        $this->name = $name;
        $this->price = $price;
        $this->createdAt = new DateTimeImmutable();
    }
}

$product = new Product('Widget', 9.99);
$product->price = 'free'; // TypeError: Cannot assign string to property Product::$price of type float

// Combined with union types (PHP 8.0):
public int|float $quantity;

// And intersection types (PHP 8.1):
public (Stringable&Countable) $value;

Typed properties work with all PHP types: scalars, arrays, classes, interfaces, nullable types, union types, and intersection types. The engine enforces them on every assignment, catching type errors immediately rather than letting corrupt data propagate through your system.

Significance: Reliability

Typed properties turn classes into enforceable contracts. An object with typed properties is always in a valid state — you can trust that $product->price is a float without checking. This cascading trust eliminates defensive type-checking throughout your codebase.