I have a class as below:
class Fields implements ArrayAccess, Countable, SeekableIterator
{
$this->_fields = array();
...continue
}
I want to have my class have the following boolean capability
$myArray = new Fields();
if($myArray == false)
{
echo 'It is empty';
}
How the above behaviour to be implemented in my class
It's not a good solution for your "problem", but it works.
class Fields implements ArrayAccess, Countable, SeekableIterator
{
protected $_fields = array();
// ...continue
public function __invoke() {
return $this->_fields == true;
}
}
I simply implemented an __invoke()
method, which gets called if the object gets used as a function. If you now do the following, it works as expected.
$myArray = new Fields();
if($myArray() == false)
{
echo 'It is empty';
}
This is probably not the best way or the way you should use __invoke
, but it's a possibility.