Show HN: ImmutableBase – Strict immutable data objects for PHP

  • Posted 5 hours ago by bill402099
  • 2 points
https://github.com/ReallifeKip/ImmutableBase
Hi HN, I built this because PHP's `readonly` keyword isn't enough. It prevents reassignment but doesn't enforce type-safe construction, nested object validation, or consistent null handling across your data layer.

ImmutableBase takes a different approach: you extend one of three base classes (DTO, VO, or SVO — each with different strictness guarantees) and all validation happens at construction time. Your objects are either valid or they don't exist.

A few things that might be interesting to this crowd: - Behavioral attributes (`#[Strict]`, `#[Lax]`, `#[SkipOnNull]`, `#[KeepOnNull]`) let you control null/missing value handling per property rather than per class - SVO (Single Value Object) supports type narrowing with automatic validation chaining — think of it as a value object that enforces its own invariants through the type hierarchy - CLI tools: `ib-cacher` for pre-warming validation cache, `ib-writer` for auto-generating Markdown docs of your entire object hierarchy - No magic methods, no naming conventions — everything is explicit and IDE-visible

Requires PHP 8.4+. I'm the sole author and maintainer. Used in production at enterprise scale in Taiwan. Happy to discuss any design decisions — there are some deliberately controversial ones in there.

Feedback welcome.

0 comments