Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sizeof(): Parameter must be an array or an object that implements Countable #79

Closed
saqueib opened this issue Nov 13, 2017 · 19 comments
Closed

Comments

@saqueib
Copy link

saqueib commented Nov 13, 2017

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

@wyxos
Copy link

wyxos commented Dec 7, 2017

Same issue here but changed condition from

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

@mateusfccp
Copy link
Contributor

mateusfccp commented Jan 3, 2018

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 #84.

@mateusfccp
Copy link
Contributor

@saqueib If possible, close the issue, as the solution has already been merged. Thanks.

@saqueib saqueib closed this as completed Jan 4, 2018
@dainemedia
Copy link

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

@winonakwok
Copy link

Yes, issue has not been fixed in dev-master. I am currently using @MM-X 's suggestion as a temporary solution

@pihlersimon
Copy link

Still getting this error.

@garethdaine
Copy link

How come this issue keeps getting close when it still occurs?

@gabriel-cardoso
Copy link

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

@iec989
Copy link

iec989 commented Feb 14, 2018

I did that @gabriel-cardoso, but now I'm getting the following error:
Class 'Countries' not found

@tomvo
Copy link

tomvo commented Feb 20, 2018

Seems to fix it for us too, could you tag this?

@wyxos
Copy link

wyxos commented Mar 1, 2018

@iec989 you may need to run composer dump-autoload

@mateusfccp late reply from me, I agree with your solution being better. Has this been merged yet? I read from the thread that it has been, but updating the package to dev-master, I get this strange error now

Non-static method Webpatser\Countries\Countries::getList() should not be called statically

@mateusfccp
Copy link
Contributor

@MM-X I made a pull request and it has been merged. Some are complaining that it didn't work, though others are saying that it did work. There doesn't seem to be anything wrong with my code, and I don't think this error you are getting has nothing to do with my code (at least the error message doesn't imply it).

Anyway, I don't know why some are not getting it to work. If you have any hint, please, comment here so we can solve it!

@wyxos
Copy link

wyxos commented Mar 5, 2018

@mateusfccp Do you confirm the merge was made on master? When I browse the repo and go to the Countries.php class I observe this at line 43

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

I have even tried removing the package from the project, deleted all files (migration + configs) reinstalled following the steps on the readme file, and now I am back to the error sizeof.

Assuming this was not merged right on dev-master or on another branch as mentioned above?

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

@wyxos
Copy link

wyxos commented Mar 5, 2018

Switching to dev-develop branch causes the same issue experienced by @iec989

@mateusfccp
Copy link
Contributor

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

@tomvo
Copy link

tomvo commented Apr 13, 2018

Can we get this merged and tagged so the library doesn't break on > PHP 7?

@hosquiat
Copy link

hosquiat commented Apr 15, 2018

I was seeing this issue as well. @mateusfccp's .php link fixed my issue.

@tomvo
Copy link

tomvo commented Sep 10, 2018

bump

1 similar comment
@webocoders
Copy link

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests