azjezz/psl
PSL is a modern, well-typed standard library for PHP 8.4+, inspired by HHVM’s HSL. It offers safer, predictable APIs for async, collections, networking, I/O, crypto, terminal UI, and robust data validation—replacing brittle built-ins with consistent alternatives.
The Result component represents a computation that either succeeded or failed. Instead of letting exceptions propagate uncontrolled, you capture the outcome as a value that can be inspected, transformed, and passed around.
This is useful when you want to defer error handling, aggregate results from multiple operations, or build transformation pipelines without scattering try/catch blocks throughout your code.
Success<T> -- Wraps a successful return value of type TFailure<T> -- Wraps a Throwable from a failed operationResultInterface<T> -- The common interface both implementThe wrap() function executes a closure and captures the outcome:
@example('types/result-wrapping.php')
map() transforms a successful value, leaving failures untouched. catch() recovers from failures, leaving successes untouched:
@example('types/result-transforming.php')
then() handles both cases at once, returning a new ResultInterface:
@example('types/result-then.php')
The proceed() method unwraps the result by calling the appropriate closure and returning the value directly (not wrapped in a Result):
@example('types/result-proceed.php')
Run cleanup logic regardless of outcome:
@example('types/result-always.php')
try_catch() wraps and recovers in a single step:
@example('types/result-try-catch.php')
reflect() converts a throwing closure into one that returns a Result:
@example('types/result-reflect.php')
When processing batches, collect_stats() summarizes outcomes:
@example('types/result-stats.php')
map() calls to build data transformation pipelinesSee src/Psl/Result/ for the full API.
How can I help you explore Laravel packages today?