Warning: sizeof(): Parameter must be an array or an object that implements Countable php7.2

帅比萌擦擦* 提交于 2020-01-25 06:44:07

问题


I updated to PHP 7.2 and it created an array (no pun intended) of issues. I've been knocking them out (mostly these sizeof and count() warnings. The one error we have :

Warning: sizeof(): Parameter must be an array or an object that implements Countable in /usr/www/domain/phpmyd/includes/class_registry.php on line 236

I tried fixing it like this :
if (sizeof($this->config) < 1) {
To this:
if (!empty($this->config) &&(sizeof($this->config) < 1)) {

But it creates lots more errors shown below, However, we fixed this one in the same way and it works perfectly. Changing this:
if (0 < sizeof($this->language)) {
To this:
if (!empty($this->language) && (0 < sizeof($this->language))) {

Took away basically the same error. Now, keep in mind, the above warning is the ONLY error left. Everything else works perfectly, however, if I do "fix" the warning, I get a bunch of errors that break the site and seem irrelevant. So, if I replace that first string all these errors appear:

  • Warning: Use of undefined constant ADDON_DISCOUNT_CODES - assumed 'ADDON_DISCOUNT_CODES' (this will throw an Error in a future version of PHP) in /usr/www/domainlistings/phpmyd/index.php on line 6
  • Warning: Use of undefined constant ADDON_BLOG - assumed 'ADDON_BLOG' (this will throw an Error in a future version of PHP) in /usr/www/domainlistings/phpmyd/cp/template/default/admin_header.tpl on line 134
  • Warning: Use of undefined constant ADDON_LINK_CHECKER - assumed 'ADDON_LINK_CHECKER' (this will throw an Error in a future version of PHP) in /usr/www/domainlistings/phpmyd/cp/template/default/admin_header.tpl on line 179

Those errors did NOT appear, and those things worked perfectly well until I changed if (sizeof($this->config) < 1) {

How is this linked? I'm not sure what is happening here, how that one line can make or break these other (seemingly irrelevant) things. Full code of inital problem (line 236):

/**
     * Get a configuration value
     * @param string $key
     * @return mixed
     */
    public function getConfig($key) {
        if (sizeof($this->config) < 1) {
            $this->loadConfig();
        }
        return isset($this->config[$key]) ? $this->config[$key] : false;
    }

Any ideas?


回答1:


For starters don't use sizeof, but count ;) cheap improvement - always one opcode less.

Secondly, make sure you pass an array, not null or whatever you have there, eg.:

// dirty fix
if (count((array) $this->config) > 0) {
    …
}

To fix this properly you should never allow this property to be null anyway. If you're after some lazy loading check if the variable is null or is not an array, eg:

public function getConfig($key, $default = false)
{
    if (!is_array($this->config)) {
        // make sure it becomes one after the load
        $this->loadConfig();
    }

    return $this->config[$key]) ?? $default;
}



回答2:


I think what's happening here is that the change you originally made ensures that loadConfig() never happens, and something in loadConfig() is responsible for defining all those constants you're getting warned about after changing that.

If you changed

if (sizeof($this->config) < 1) {

to

if (!empty($this->config) &&(sizeof($this->config) < 1)) {

then if $this->config was null (the default value of an object property that has not yet been given a value), then the second one would mean the if condition wouldn't be satisfied because null is empty, where in the first one, it would be satisfied because sizeof(null) still returns 0 even though it gives you the uncountable warning.

sizeof was never really necessary there. You don't have to count something just to see if it exists.

I think this should work just fine, and make more sense for what it's actually meant to do.

if (empty($this->config)) {
    $this->loadConfig();
}


来源:https://stackoverflow.com/questions/58944243/warning-sizeof-parameter-must-be-an-array-or-an-object-that-implements-count

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!