Last active
December 11, 2017 22:05
-
-
Save sirbrillig/35993761ffff989d1f9e9f9d34c66216 to your computer and use it in GitHub Desktop.
A simple implementation of a `Maybe` return class in PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should the default constructor be private?