tags:

views:

84

answers:

3

I've encountered a very odd issue in a class I'm creating. Here is a snippet of the class below and it's output:

class WeirdHappenings
{
    protected $filters_list = array();

...

    function build()
    {
        $filters_count = count($this->filters_list);

        echo "<pre>";
        var_dump($this->filters_list);
        echo "<br>" . $filters_count . " is the count";
        echo "</pre>";
    }
}

Before you ask, yes filters_list is a populated array which is populated during the execution of the class. The Vardump proves that:

array(2) {
  ["filter_1"]=>
  string(17) "calendar year nbr"
  ["filter_2"]=>
  string(18) "reviewer type desc"
}

0 is the count

How can this be possible? It's an array with two elements yet count can't tell me how big it is?

PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

error_reporting = E_ALL | E_STRICT

This is the resolution:

class WeirdHappenings
{
    protected $filters_list = array();
    protected $thing = array("foo" => "bar", "ack" => "bar");

    function WeirdHappenings()
    {

    }

    function makeMeCry()
    {
        $filters = array();
        $filter_count = 1;

        $crapola = array("f1" => array("name" => "calendar year nbr"), "f2" => array("name" => "reviewer type desc"));

        foreach( $crapola as $key => $data )
        {
            $filters["filter_$filter_count"] = $data['name'];
            $filter_count++;
        }

        $this->filters_list = $filters;
    }

    function build()
    {       
        $filters_count = count($this->filters_list);
        $this->makeMeCry();
        echo "<pre>";
        var_dump($this->filters_list);
        var_dump($this->thing);
        echo "<br>" . $filters_count . " is the count of the filters";
        echo "<br>" . count($this->thing) . " is the count of the thing";
        echo "</pre>";
    }
}

$weirdthings = new WeirdHappenings();
$weirdthings->build();

As pointed out in numerous comments the count was being performed prior to the population of the array.

+1  A: 
echo "<pre>";
var_dump($this->filters_list);
echo "<br>" . count($this->filters_list) . " is the count";
echo "</pre>";

i'm sure this would return 2. if not there is probably some "php magic" going on with __get(), __set(), __sleep() or something like that

antpaw
__get and __set wouldn't be called from inside a class, since the variable exists and is accessible. __sleep() is completely unrelated.
Mewp
It does not return two. __get and __set are not being used within the Class. Quite literally there is an array and it's not being counted.
Marco Ceppi
+1  A: 

PHP count() returns zero only in two cases:

echo count(null); // returns 0
echo count(array()); // returns 0

So, 1) Check your spelling; 2) Turn on error reporting:

error_reporting(E_ALL);
ini_set('display_errors', 'on');

3) Try to run your code on another version of PHP (without Suhosin-Patch).

Sergiy
A: 

This was a programming error on my part. Where I was counting an array prior to populating it.

Marco Ceppi