Created
October 2, 2015 17:59
-
-
Save alanwillms/b01ae92e4f41576b9d81 to your computer and use it in GitHub Desktop.
Fail fast to avoid dealing with null
This file contains hidden or 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 | |
declare(strict_types=1); | |
class UserException extends Exception { } | |
class Application | |
{ | |
... | |
public function getUser() : User | |
{ | |
if (empty($this->identity)) { | |
throw new UserException('User is not authenticated.'); | |
} | |
return $this->identity; | |
} | |
} | |
try { | |
echo "Olá, " . $app->getUser()->name, "!"; | |
} catch (UserException $e) { | |
echo "Olá!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment