Page 1 of 1

PHP5 empty object check

Posted: Tue Apr 17, 2007 3:28 pm
by ed209
As of PHP 5, objects with no properties are no longer considered empty.

So,

Code: Select all

if ( !empty( $myObject ) ) {
     // object not empty
}
will not work - even if it is empty. Is there a way around this?

Posted: Tue Apr 17, 2007 3:47 pm
by John Cartwright

Posted: Tue Apr 17, 2007 3:54 pm
by ed209
so something like:

Code: Select all

if ( !count(get_object_vars( $myObject )) ) {
    // object is empty
}
I think the PHP4 method was nicer! Thanks for the reply.

Posted: Tue Apr 17, 2007 3:59 pm
by John Cartwright
I'm actually not sure how it plays with protected and private properties, so you may have to test that. Better yet, let us know of the results.

If all else fails, theres always Reflection.

Posted: Tue Apr 17, 2007 4:08 pm
by ed209
I'm afraid you lost me :oops:

The above does work for what I need though, thanks. My object is pretty simple, just a load of $_POSTed values put into an object.