Created
April 15, 2023 17:20
-
-
Save atakde/e205df83bdda4ebe5d8e2e8e90217dbf to your computer and use it in GitHub Desktop.
PHP Guard Clause
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 | |
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