Skip to content

Instantly share code, notes, and snippets.

@taueres
Last active June 13, 2016 11:07
Show Gist options
  • Save taueres/66a1d767e6aff5a76e7e8518dbca503c to your computer and use it in GitHub Desktop.
Save taueres/66a1d767e6aff5a76e7e8518dbca503c to your computer and use it in GitHub Desktop.
<?php
class Optional
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function map(callable $func)
{
if (null === $this->value) {
return $this;
}
$result = $func($this->value);
return new Optional($result);
}
public function flatMap(callable $func)
{
if (null === $this->value) {
return $this;
}
return $func($this->value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment