0

I have a class that looks like the following (shortened):

class Request
{


    /**
     * @var array $get get request, singleton
     */
    private static $get = [];

    /**
     * Returns an instance of the request
     *
     * @param string $string request type to return
     *
     * @return array instance of request array
     */
    public static function getInstance($string)
    {
        switch ($string) {
            case 'get':
                if (count(static::$get) === 0) {
                    static::$get = filter_input_array(INPUT_GET);
                }

                return static::$get;
                break;

           }
    }
}

The line if (count(static::$get) === 0) { throws a warning

Warning: count(): Parameter must be an array or an object that implements Countable

I am aware of the changes in PHP 7 and above, but I don't understand why static::$get is not known as an array, which should be countable. Where do I think wrong? It's an empty array, so counting it should account to 0?

Patrick
  • 691
  • 8
  • 30
  • 2
    `filter_input_array` doesn't necessarily return an array, so after calling this once you don't necessarily have something countable in `$get`. When you then call `getInstance` again you can end up with that warning. – user3942918 Nov 08 '18 at 16:57
  • I did not know this. That seems to be inconsistent on behalf of PHP. – Patrick Nov 08 '18 at 17:01

0 Answers0