<?php
use PhpOption\None;
use PhpOption\Some;
class Before
{
/**
* @param mixed $param
* @return null|int
*/
public function find($param)
{
$value = null;
if (!is_null($param)) {
// result of your search by param
$value = 1;
}
// 0 can be a result, null means nothing found
return $value;
}
}
class WithPhpOption
{
/**
* @param mixed $param
* @return PhpOption\Option
*/
public function find($param)
{
$value = None::create();
if (!is_null($param)) {
// result of your search by param
$value = new Some(1);
}
return $value;
}
}
$obj = new Before();
$value = $obj->find(null);
if (is_null($value)) {
$value = 0;
}
echo $value . PHP_EOL;
$obj = new WithPhpOption();
echo $obj->find('mySearchValue')
->getOrElse(0) . PHP_EOL;
echo $obj->find(null)
->getOrElse(0) . PHP_EOL;