- How do I install php-standard-library/option in a Laravel project?
- Run composer require php-standard-library/option. It is a native PHP package with no Laravel dependencies, so no package service provider is required. Laravel's Composer autoloading makes the Option classes available.
- What is the difference between Option and nullable types in Laravel?
- Option makes absence explicit as None and presence as Some, while nullable types use null for absence. This removes nested isset/null checks and makes Some/None visible in method signatures. You can chain map, flatMap, and filter operations without repeated null guards.
- Does php-standard-library/option integrate with Laravel Eloquent?
- It can, but Eloquent does not return Option automatically. Wrap nullable query results with Option::fromNullable($model) and adapt legacy null-returning code at boundaries. Dynamic Eloquent properties may need wrappers rather than direct Option chaining.
- Which PHP and Laravel versions does php-standard-library/option support?
- The package requires PHP 8.0 or newer, which is needed for features such as match expressions. It has zero Laravel dependencies, so Laravel version compatibility is mainly determined by your app's PHP and Composer constraints. Check the package's composer.json for the exact supported ranges.
- How do I wrap an Eloquent model in an Option?
- Use Option::fromNullable($user) when the query result may be null; it yields Some for a model and None for null. Then chain map or filter for optional business logic instead of nested if checks. Keep raw Eloquent calls at the boundary and convert to Option before passing into domain code.
- Can Option replace nullable request input handling in Laravel controllers?
- You can wrap optional input with Option::from($request->input('optional_field')) to make absence explicit as None. This helps distinguish not provided from validation failures, which should remain 400-style responses. Keep Laravel validation for invalid data and use Option for presence semantics in your domain or API layer.
- How should I test Option values in Laravel tests?
- Assert presence explicitly, for example assertTrue($option->isSome()) or assertTrue($option->isNone()). Update tests that previously expected null to expect None and cover nested Option and missing-value edge cases. Add feature tests around request and Eloquent boundaries where you convert null to Option.
- Is php-standard-library/option suitable for production Laravel applications?
- It is a native PHP package with negligible runtime overhead compared with null checks; Some cases add slight object-wrapping memory that is usually insignificant at scale. It fits production code where explicit presence semantics reduce ambiguity, especially in domain and API layers. Benchmark critical paths and use it selectively rather than across all simple CRUD.
- How do I configure PHPStan to work with Option types in Laravel?
- Configure PHPStan to expect Option return types where nullable types were previously allowed. Add custom PHPStan or Nikita rules to flag null returns and enforce Option adoption, as native tooling may not infer all Option semantics. Expect more limited IDE autocompletion for Option methods than for native PHP types.
- What are alternatives to php-standard-library/option and when should I avoid it?
- Alternatives include plain nullable type hints with PHPStan ignores, or a hybrid approach using Option for domain models and null for simple APIs. Avoid Option for trivial CRUD forms where null is sufficient, and beware the cognitive load of monadic patterns like flatMap. Teams without functional-pattern experience should start with a targeted proof of concept.