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.