0

I would like to check, if any of the properties of an object is empty, without manually checking each one. This is how I do it now:

class MyClass {
    public $a;
    public $b;
    public $c;
}

Then:

$item = new MyClass();

// ...
$item->a = 10;
$item->b = 20;
// ...

// TODO:
$hasEmpty = empty($item->a) || empty($item->b) || empty($item->c);

How is it possible without manually checking each property?

Iter Ator
  • 8,226
  • 20
  • 73
  • 164
  • First you should define what you mean by "empty". The function `empty()` considers multiple values as empty (like null, empty string, the number 0 (as an int or a string), an empty array, false) – M. Eriksson May 10 '22 at 12:47
  • Simply use array_filter(). Details in this post... https://stackoverflow.com/questions/43020364/php-remove-empty-values-in-array#43020411 – MisterG13 May 10 '22 at 12:52
  • @MisterG13 - How would you use `array_filter()` on an object? The question you liked to uses arrays, not objects. – M. Eriksson May 10 '22 at 12:53
  • One of the answers shows this usage... get_object_vars() https://www.php.net/manual/en/function.get-object-vars.php ... Couple more examples https://www.educba.com/php-object-to-array/ – MisterG13 May 10 '22 at 12:55

4 Answers4

2

You can actually typecast object to an array and then use array_filter on it to filter out the empty values and check it's size like below:

<?php

$hasEmpty = count(array_filter((array)$item,fn($v) => empty($v))) > 0;

var_dump($hasEmpty);

Online Demo

Another way could be by implementing ArrayAccess interface and accessing object keys just like array keys and performing the empty checks, but I will leave it upto the reader to implement them as an exercise if they wish to.

nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • 1
    for anyone needing a bit more info on what typecasting is and how it works, check out this link. Scroll to about 3/4th of the way down to "Casting an Object to an Array". https://code.tutsplus.com/tutorials/php-type-casting-a-complete-guide--cms-38764 – MisterG13 May 10 '22 at 14:30
1

I suggest destructuring your object and then loop through and see if any are null.

// create class
class MyClass {
    public $a;
    public $b;
    public $c;
}

// create object
$item = new MyClass();
$item->a = 10;
$item->b = 20;

// get vars and test if any properties are null
$vars = get_object_vars($item);
$isNull = false;
foreach($vars as $var){
    if(empty($var)){
       $isNull = true;
    }
}

or you could turn this into a function

function areAnyPropertiesEmpty($class){
     $vars = get_object_vars($class);
    $isNull = false;
    foreach($vars as $var){
       if(empty($var)){
           $isNull = true;
       }
    }
    return $isNull;
}
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
0

I think you have to loop through the object.

function isObjectEmpty($obj) {
  foreach ($obj as $value) {
    if (!isset($value)) {
      return true;
    }
  }
  return false;
}
paulin-crtn
  • 325
  • 2
  • 9
0

The best way is to use PHP Reflection. This sample code checks ALL properties:

<?php

$item = new MyClass();

$reflect = new ReflectionClass($item);

$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

$is_empty = false;

foreach ($props as $prop) {
    if (empty($prop->getValue())) {
        $is_empty = true;
        // We break, since it's useless to continue over
        break;
    }
}

// Test it
var_dump($is_empty);

?>
Luca Murante
  • 317
  • 3
  • 8