phpoption/phpoption
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The phpoption/phpoption is a package that implements the Option type for PHP programming language. Its primary function is to handle cases where a return type might vary based on certain conditions or runtime factors. It helps prevent accidental omissions in code handling by forcing a developer to consider all possible return cases. This improves code robustness and facilitates a more concise and empowered API development and usage.
The phpoption/phpoption can be employed in your PHP applications through a straightforward installation and usage process. To install, you can add it directly to your project using Composer, either by requiring it in your terminal:
$ composer require phpoption/phpoption
Or by manually adding it to your composer.json
file.
Usage of phpoption/phpoption package involves creating a new Option, either a Some or None instance, based on whether an entity could be found or not.
To create a new Option:
class MyRepository
{
public function findSomeEntity($criteria): \PhpOption\Option
{
if (null !== $entity = $this->em->find(...)) {
return new \PhpOption\Some($entity);
}
// We use a singleton, for the None case.
return \PhpOption\None::create();
}
}
You can use a shorter version, considering null
as None
and everything else as Some
case:
class MyRepository
{
public function findSomeEntity($criteria): \PhpOption\Option
{
return \PhpOption\Option::fromValue($this->em->find(...));
// or, if you want to change the none value to false for example:
return \PhpOption\Option::fromValue($this->em->find(...), false);
}
}
The primary documentation for the phpoption/phpoption package is available directly within its GitHub repository. Extensive code examples and explanations are provided directly in the README file for you to understand the concepts and usage of the package. Additional resources regarding security, license and enterprise subscription can also be found there.