Skip to content

Instantly share code, notes, and snippets.

@sirbrillig

sirbrillig/Maybe.php

Last active Dec 11, 2017
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@nbloomf nbloomf commented Nov 11, 2017

Should the default constructor be private?

@Hywan

This comment has been minimized.

Copy link

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