1

Is there a way to define the boolean returned value for an if statement issued on my class instances? I have a class implementing the array access interface and so on, but even if the wrapped array is empty:

if($class)

will always return true, since it is an object and does exists. I'd rather not have to issue everytime an:

if(count($class))

Is there any way to achieve this?

Damiano Barbati
  • 3,356
  • 8
  • 39
  • 51

4 Answers4

2

If I get the point you can implement the countable interface; This requires you define the count() method, which let you know (by using it like count($myArrayAccessInstance)) if your internal array has items or not o whatever logic you want to define.

class MyAy implements ArrayAccess, Countable
{
    private $data;

    public function offsetExists($key)
    {
         # code...
    }

    public function offsetUnset($key)
    {
         # code...
    }

    public function offsetGet($key)
    {
        # code...
    }

    public function offsetSet($key, $value)
    {
        $this->data[$key] = $key;
    }

    public function count()
    {
        return $this->data > 0;
    }
}

$my = new MyAy();

$my['user'] = "Ilpaijin";

if(count($my))
{
    var_dump($my);
}
ilpaijin
  • 3,645
  • 2
  • 23
  • 26
1

Given that you are implementing a class, maybe you could have a member which counts the elements in the array? Something like:

class MyClass {

    public $numElems = 0;
    private $elems = array();

    ...

    public function add($elem)  {
        $elems[] = $elem;
        $numElems++;
    }

    ...
}

And then, do the iflike if($class->numElems) ...

lpg
  • 4,897
  • 1
  • 16
  • 16
  • But then I have to keep track of the count everytime I modify the wrapped array or use a (slow as hell) magic method to access dinamically that property calculated everytime on the fly. mmmm, I'll keep it in mind anyway :) – Damiano Barbati Aug 03 '14 at 13:10
  • @lpg Then would be better to implement and use count method: `public function count() { return count($elems); }` – hindmost Aug 03 '14 at 13:23
  • @lpg Why reinvent something that does already exist? http://de1.php.net/manual/en/spl.iterators.php – Daniel W. Aug 03 '14 at 13:31
1

Its not possible

In PHP an object when cast to bool always produces true. There is no way of changing that.

Check answers here

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
1

It's impossible to cast an object to a boolean related to a property inside the class because it always will return true. but I think you just need to create a function inside your class that allows you to return the boolean you need like this

public function bool()
{
    return (count($this->array)>0);
}

when you need to use it, just call the function like this if ($obj->bool()) { ... } and another way to make your statements look like what you wanted to do in the begining is to define a class with a really short name, I usually use _, and this function should return the boolean you need just like this

function _ ($Obj)
{
    return $Obj->bool();
}

finally, instead of testing on your class like this if ($class->bool()) which is a bit long, you do it like this

if (_($class))
{ /* do something fun */ }
Khalid
  • 4,730
  • 5
  • 27
  • 50