Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: how to check if an object's properties have values?

I use this to check if an object has properties,

function objectHasProperty($input){
        return (is_object($input) && (count(get_object_vars($input)) > 0)) ? true : false;
 }

But then I want to check further to make sure all properties have values, for instance,

stdClass Object
        (
            [package] => 
            [structure] => 
            [app] => 
            [style] => 
            [js] => 
        )

Then I want to return false if all the properties have empty values. Is it possible? Any hint and ideas?

like image 901
Run Avatar asked Nov 29 '22 00:11

Run


1 Answers

There are several ways of doing this, all the way up to using PHP's reflection API, but to simply check if all public properties of an object are empty, you could do this:

$properties = array_filter(get_object_vars($object));
return !empty($properties);

(The temporary variable $properties is required because you're using PHP 5.4.)

like image 68
George Brighton Avatar answered Dec 01 '22 14:12

George Brighton