Created
July 18, 2017 15:58
-
-
Save strangerstudios/00381b1d1c1c52a1c067818fb36a6e0c to your computer and use it in GitHub Desktop.
Example where checking if a property of an object is empty fails when magic methods are involved.
This file contains 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
//this check will fail if $user->user_email is not set already | |
//it usese a PHP "magic method" to fetch the value, but | |
//the empty check will return true instead of actually | |
//checking the value | |
if ( empty( $user->user_email ) ) //in a lot of cases $user->user_email is always empty, even if the user has email address | |
return; | |
//this check will work no matter what | |
$email = $user->user_email; | |
if (empty($email)) | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment