petrknap/optional
Optional brings Java 8-style Optional to PHP, making null handling explicit and safe with isPresent(), get(), orElse(), and ifPresent(). Create strictly typed optionals for bools, strings, arrays, objects, and custom types using built-in traits with full static analysis support.
null, a sentinel, an exception, or an explicit Optional<T> remains a product-level decision about how loudly the system forces callers to handle absence. This package is still the "force it" option, but v4.0.0-rc1 changes the cost/benefit calculus.TypedOptional helper and removes Optional::toNullable. Those removals eliminate much of the "buy" justification cited previously (typed-trait helpers and convenience interop). Treat this package as adopt-as-reference or fork first; dependency adoption is now harder to defend unless you specifically want Java-parity semantics and are prepared to absorb major-version churn.equals, flatMap, map, filter, ifPresent, getters). If already on v3, this is a roadmap item: pin v3, test v4 in a branch, and budget migration work. Do not start new work on v4.0.0-rc1 without an explicit pre-release policy.find* methods, cache reads, optional configuration, plugin/hook resolution — where a miss is normal and null invites silent null-propagation.Optional remains a recognizable contract. The v4 template covariance change improves subtyping ergonomics but may require static-analysis updates.Result/Either type — do not use it for failure, validation errors, or reasons. It is not a substitute for typed properties, DTO validation, or non-nullable domain invariants. The removal of toNullable means you must choose a deliberate boundary-unwrapping convention rather than relying on a convenience method.Consider adoption when all of the following hold:
@var Optional<Foo> docblocks). Without this, the wrapper degrades into untyped ceremony.Optional-like helper you are consolidating).Optional::toNullable and TypedOptional are removed. Confirm your boundary conversion and type-helper needs are covered by remaining APIs (orElse, get, etc.) or by internal wrappers.Look elsewhere when:
?Type return types, ??, and ?-> already express the contract. For most codebases the language feature is cheaper and more idiomatic.Result/Either or a small value object, not Optional.Optional::of() allocates; in tight loops and serialization layers this is measurable overhead for zero correctness gain.TypedOptional or toNullable was central to your design, stay on v3 with a fork plan or choose a different abstraction. Do not adopt v4.0.0-rc1 expecting those helpers to return.For executives (one paragraph): This remains a small, low-cost coding convention that makes "this value might be missing" impossible to ignore, reducing a recurring class of production bugs. However, the new v4.0.0-rc1 is a major breaking release: it removes the TypedOptional helper and the toNullable method, changes template variance, and adjusts core semantics (equals, flatMap, map, filter, ifPresent, getters). That weakens the case for taking it as a dependency and strengthens the case for forking or staying on v3 until v4 stabilizes. Licensing risk is unchanged: LGPL-3.0 needs legal clearance before commercial distribution. Recommendation: do not adopt v4.0.0-rc1 org-wide; approve a scoped pilot on v3 or a vendored/forked core, and budget a day for legal review plus a migration spike for v4.
For engineering leads: v4.0.0-rc1 buys Java-parity semantics and covariance, but it removes TypedOptional and toNullable, which were previously part of the "buy" justification. If you already use those, v4 is a regression unless you add internal helpers. The equals/flatMap/map/filter/ifPresent alignment is good for correctness but requires test coverage before upgrading. Keep Optional constrained to repository/SDK boundaries, keep domain internals on native nullable types, and never use it for errors. Given the ~single-file core and the RC status, plan to vendor or fork so an upstream stall or another major release is a non-event.
Ask: a two-week, one-module pilot with a written rule for where Optional is allowed, a static-analysis gate, legal sign-off on LGPL-3.0, and an explicit upgrade decision: pin v3 now, test v4.0.0-rc1 in a branch, and pre-agree to fork the core internally if v4 stabilization or the removed helpers become blockers.
How can I help you explore Laravel packages today?