laravel-countries: sizeof(): Parameter must be an array or an object that implements Countable

I am getting this error while seeding the countries

ErrorException: sizeof(): Parameter must be an array or an object that implements Countable in ../../vendor/webpatser/laravel-countries/src/Webpatser/Countries/Countries.php:43

If I remove the sizeof check it works.

//Get the countries from the JSON file
// if (sizeof($this->countries) == 0){
    $this->countries = json_decode(file_get_contents(__DIR__ . '/Models/countries.json'), true);
//}

//Return the countries
return $this->countries;

initializing protected $countries = []; with empty array can fix it.

I am running PHP 7.2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 19 (4 by maintainers)

Most upvoted comments

This issue is still occurring and has not been fixed in dev-master.

It’s merge in develop branch. It works if you specify "webpatser/laravel-countries": "dev-develop" in your composer.json

Same issue here but changed condition from

if (sizeof($this->countries) == 0){ to if (!$this->countries || sizeof($this->countries) == 0){

@mm-x I merged on develop, as it can be seen here: https://github.com/webpatser/laravel-countries/blob/develop/src/Webpatser/Countries/Countries.php

I don’t know if it should’ve been merged in another branch, for me it makes sense to merge to develop instead of master.

Still getting this error.

I did the same as @mm-x and it worked.

A cleaner solution would be to use if (empty($this->countries)){, as according to documentation, when using empty(),

A variable is considered empty if it does not exist or if its value equals FALSE.

and

The following things are considered to be empty:

  • “” (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • “0” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Thus, they are equivalent expressions.

This solution has been requested on pull-request https://github.com/webpatser/laravel-countries/pull/84.