Skip to content

Instantly share code, notes, and snippets.

@atakde
Created April 15, 2023 17:20
Show Gist options
  • Save atakde/e205df83bdda4ebe5d8e2e8e90217dbf to your computer and use it in GitHub Desktop.
Save atakde/e205df83bdda4ebe5d8e2e8e90217dbf to your computer and use it in GitHub Desktop.
PHP Guard Clause
<?php
class TestClass
{
public function isUsernameStartsWithLetter($username)
{
// if empty string trow exception
if (empty($username)) {
throw new Exception('Username is empty');
}
return preg_match('/^[a-zA-Z]/', $username);
}
}
$obj = new TestClass();
var_dump($obj->isUsernameStartsWithLetter('')); // exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment