Skip to content

Instantly share code, notes, and snippets.

@alanwillms
Created October 2, 2015 17:59
Show Gist options
  • Save alanwillms/b01ae92e4f41576b9d81 to your computer and use it in GitHub Desktop.
Save alanwillms/b01ae92e4f41576b9d81 to your computer and use it in GitHub Desktop.
Fail fast to avoid dealing with null
<?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