1

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.

14
  • 2
    Have you set error_reporting to E_ALL | E_STRICT? If not, please do so. Commented Jul 20, 2010 at 22:54
  • @nikic Error reporting has been set to E_ALL | E_STRICT and still no errors reported. Commented Jul 20, 2010 at 23:02
  • Instead of using ... (and thus hiding the probable culprit) try to provide a minimal reproducing code sample. Usually in the process of generating such a sample the error will get revealed. Commented Jul 20, 2010 at 23:06
  • 2
    @Marco Ceppi: We can't paste your code and run it to reproduce the behavior. As long as we can't reproduce it, we can't know what's wrong. There has to be something else Commented Jul 20, 2010 at 23:09
  • 2
    Most likely you have an error somewhere else. The code above will say the count is 2 for sure. Commented Jul 20, 2010 at 23:15

3 Answers 3

1
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

Sign up to request clarification or add additional context in comments.

2 Comments

__get and __set wouldn't be called from inside a class, since the variable exists and is accessible. __sleep() is completely unrelated.
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.
1

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).

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.