Force result for empty() test on an object

Posted by hsz on Stack Overflow See other posts from Stack Overflow or by hsz
Published on 2010-06-16T10:36:02Z Indexed on 2010/06/16 10:52 UTC
Read the original article Hit count: 177

Filed under:
|
|

Hello !

Simple class for example:

class Foo
{
    protected $_bar;

    public function setBar( $value ) {

        $this->_bar = $value;

    }

}

And here is the question:

$obj = new Foo();

  var_dump( empty( $obj ) );  // true

$obj->setBar( 'foobar' );

  var_dump( empty( $obj ) );  // false

Is it possible to change class's behaviour with testing it with empty() function so it will returns true when object is not filled with data ?

I know about magic function __isset( $name ) but it is called only when we test specific field like:

empty( $obj->someField );

but not when test whole object.

© Stack Overflow or respective owner

Related posts about php

Related posts about oop