0

Looking for best way to accomplish below. I want to return TRUE if ANYTHING is empty in my user object, I don't care which one is empty. I think I've done this before with searching for empty values in an array but can't think of the best way to do this with objects.

I know I can loop thru the object and break once I find an empty value in the object, but checking first to see if there is a better way of doing this, thanks!

function is_it_empty($user)
{
 $is_it_empty = FALSE;
 if( isset($user->first_name) )
   $is_it_empty = TRUE;

 if( isset($user->last_name) )
   $is_it_empty = TRUE;

 return $is_it_empty;
}

also:

function name($user)
{
 foreach($user as $u):
   if( isset ($user->value) ): // or should it be isset?
      return true;
      break;
   endif;
 endforeach;

 return true;
}
4
  • what does $user object look like ? Commented Jul 23, 2013 at 17:57
  • $user->first_name, $user->last_name. I hope that answers it. Commented Jul 23, 2013 at 17:58
  • empty is used for arrays. For other values you should use isset. Commented Jul 23, 2013 at 17:59
  • 1
    @Maximus2012 That is incorrect. empty() checks if a variable "does not exist or if its value equals FALSE". empty($var) is equivalent to !isset($var) || $var == false. An array with zero elements is "empty" by this definition, and so is a string with zero characters, and the integer zero. Commented Jul 23, 2013 at 18:36

4 Answers 4

1
$user = new stdClass();
$user->firstName = 'Mark';
$user->lastName = 'Baker';
$user->sanity = NULL;

function is_it_empty($user) {
    foreach($user as $property) {
        if (empty($property)) {
            return true;
        }
    }
    return false;
}

var_dump(is_it_empty($user));
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this function will only work with public properties. See my answer to see into private and protected properties as per OP requirements. On the other hand i am very interested in how to inspect private/protected properties without using reflection!
Where's the OP's requirement that it should work for protected or private properties? I missed that
I probably misinterpreted ...I don't care which one is empty. :-/ Anyways, he can choose whichever function gets the job done!
0

u can use foreach. it's better than ur code.

        function is_it_empty($user)
        {
        foreach($user as $key=>$value)
        {
         if( empty($value) ){
           return = TRUE;
        }
}
    }

Comments

0

In case any one ever happens to care about encapsulation, this code might come in handly. The first function inspect public attributes only.

function is_prop_empty( $object ) {
$reflect = new ReflectionClass($object);
$props   = $reflect->getProperties();

foreach ($props as $prop) {
    try {
        $result = $prop->getvalue( $object );

        if( $result === null ) {
            return true;
        }
    } catch( Exception $e ) {
    }
}

return false;
}

var_dump( is_prop_empty($user) );

And with this second one, you can look into private and protected properties:

function is_prop_empty( $object ) {
        $reflect = new ReflectionClass($object);
    $props   = $reflect->getProperties();

    foreach ($props as $prop) {     
        $prop->setAccessible(true);
        $result = $prop->getvalue( $object );
        if( $result === null ) {
            return true;
        }
    }

    return false;
}

EDIT

By empty, i assumed you meant null but you can modify the function(s) above to meet your requirements of "empty".

Comments

0

I think having this generic function to check if a value exists would do the job

function is_empty($data) {
  if(empty($data) || !isset($data) || $data == false) {
    return true;
  }
  return false;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.