- What is graham-campbell/result-type used for?
- It is a lightweight, framework-agnostic PHP implementation of the Result type that exposes Ok and Err value objects, letting you represent failure as a returned value instead of a thrown exception. It fits domain and service layers where failure is an expected business outcome, such as payment declines, third-party API rejections, or parsing external payloads. It is not a replacement for Laravel's exception-driven HTTP kernel, validation, middleware, queue retries, or Eloquent.
- How do I install graham-campbell/result-type?
- Install it with Composer using composer require "graham-campbell/result-type:^1.1". There is no service provider, no config file to publish, and no migrations, so integration cost is effectively zero. The package requires PHP 7.2.5 through 8.6 and pulls in phpoption as a small transitive dependency, so pin and audit your lock file rather than treating it as dependency-free.
- Which PHP and Laravel versions does graham-campbell/result-type support?
- The package supports PHP 7.2.5 through 8.6 and tracks new PHP releases promptly. Because it is framework-agnostic and contains no Laravel-specific code, it works with any Laravel version running a supported PHP release. Just confirm your Laravel install is compatible with your chosen PHP version.
- Is graham-campbell/result-type a replacement for Laravel exceptions?
- No. It is a complementary pattern meant to be confined to the service or domain layer, with adapters at the edge that translate to and from exceptions. Laravel's controllers, FormRequests, middleware, jobs, and Eloquent all speak exceptions, and mixing the two paradigms in one call stack produces confusing traces and inconsistent error contracts. If your team cannot enforce that boundary, the package will reduce clarity rather than improve it.
- Do I need a service provider or config file for graham-campbell/result-type?
- No. It is a plain Composer library with no service provider, no publishable configuration, no migrations, and no runtime hooks. The real adoption cost is convention, adapter code, and refactoring effort, not installation. Greenfield service classes are the cheapest and lowest-risk place to start.
- How do I use Ok and Err values in practice?
- Your code returns an Ok instance on success and an Err instance on failure, and the caller inspects the result and unwraps it deliberately. Because PHP lacks exhaustive pattern matching on objects, structural equality, and destructuring, a carelessly unwrapped Err becomes a runtime failure, which is exactly the failure mode the pattern exists to prevent. Pair the package with static analysis so ignored results are caught before production.
- Can I use Result types in Laravel controllers, models, or jobs?
- It is strongly discouraged. Those layers are built around exceptions, so adopting Result there requires translation shims and creates a second, competing error-handling paradigm. The recommended entry point is a greenfield service class or an external-API client in one bounded context, with controllers, models, and jobs explicitly out of scope for the pilot.
- Is graham-campbell/result-type well maintained and production ready?
- Maintenance risk is genuinely low: it is MIT licensed, has a disciplined changelog and release history, tracks PHP versions promptly, and offers commercial support through Tidelift. The bus factor is one maintainer, but the surface area is small enough to audit and fork in an afternoon. Real-world usage in Laravel applications is thin, with zero dependents and a modest composite score, so a scoped pilot is wiser than a global mandate.
- What are alternatives to graham-campbell/result-type?
- The main alternative is Laravel's native exception handling, which is the right choice for controllers, validation, middleware, queue retries, and Eloquent. You can also model outcomes with typed error enums, dedicated DTOs, or simple nullable and boolean returns, depending on how much structure your error taxonomy needs. This package suits teams that want a minimal Ok/Err pair without pulling in a larger functional-programming library.
- How do I make sure Err values are handled in CI?
- Configure PHPStan or Psalm to understand the Ok/Err hierarchy and flag ignored or unwrapped Result values, and treat those findings as CI failures. Without a mechanical gate, the convention will erode quickly under review pressure. Define your error taxonomy for Err, such as string codes, backed enums, or typed error objects, and assign a named owner for the exception-to-Result adapters used around third-party SDKs.