Skip to content

Instantly share code, notes, and snippets.

@sirbrillig
Last active December 11, 2017 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirbrillig/35993761ffff989d1f9e9f9d34c66216 to your computer and use it in GitHub Desktop.
Save sirbrillig/35993761ffff989d1f9e9f9d34c66216 to your computer and use it in GitHub Desktop.
A simple implementation of a `Maybe` return class in PHP.
<?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;
}
}
@nbloomf
Copy link

nbloomf commented Nov 11, 2017

Should the default constructor be private?

@Hywan
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