Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strangerstudios/00381b1d1c1c52a1c067818fb36a6e0c to your computer and use it in GitHub Desktop.
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 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