Skip to content

Instantly share code, notes, and snippets.

Last active December 11, 2017 22:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
A simple implementation of a `Maybe` return class in PHP.
class Maybe {
private $value;
private $error;
private function __construct($value, $error) {
$this->value = $value;
$this->error = $error;
public static function fromError($error): Maybe {
return new Maybe(null, $error);
public static function fromValue($value): Maybe {
return new Maybe($value, null);
public function isError(): bool {
return $this->error === null;
public function getError() {
return $this->error;
public function getValue() {
return $this->value;
Copy link

nbloomf commented Nov 11, 2017

Should the default constructor be private?

Copy link

Hywan commented Nov 13, 2017

Yes, it must.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment