简体   繁体   English

警告:sizeof():参数必须是一个数组或实现 Countable php7.2 的 object

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

I updated to PHP 7.2 and it created an array (no pun intended) of issues.我更新到 PHP 7.2,它创建了一个问题数组(没有双关语)。 I've been knocking them out (mostly these sizeof and count() warnings. The one error we have:我一直在将它们淘汰(主要是这些 sizeof 和 count() 警告。我们遇到的一个错误:

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警告:sizeof():参数必须是数组或 object,它在 /usr/www/domain/phpmyd/includes/class_registry.php 中实现 Countable 在第 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警告:在第 6 行的 /usr/www/domainlistings/phpmyd/index.php 中使用未定义的常量 ADDON_DISCOUNT_CODES - 假定为“ADDON_DISCOUNT_CODES”(这将在 PHP 的未来版本中引发错误)
  • 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警告:在第 134 行的 /usr/www/domainlistings/phpmyd/cp/template/default/admin_header.tpl 中使用未定义的常量 ADDON_BLOG - 假定为“ADDON_BLOG”(这将在 PHP 的未来版本中引发错误)
  • 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警告:在第 179 行的 /usr/www/domainlistings/phpmyd/cp/template/default/admin_header.tpl 中使用未定义的常量 ADDON_LINK_CHECKER - 假定为“ADDON_LINK_CHECKER”(这将在 PHP 的未来版本中引发错误)

Those errors did NOT appear, and those things worked perfectly well until I changed if (sizeof($this->config) < 1) {这些错误没有出现,并且在我更改之前,这些东西运行良好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):初始问题的完整代码(第 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?有任何想法吗?

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.我认为这里发生的事情是您最初所做的更改确保loadConfig()永远不会发生,并且loadConfig()中的某些内容负责定义您在更改后收到警告的所有常量。

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.那么如果$this->confignull (尚未赋予值的 object 属性的默认值),那么第二个将意味着不满足 if 条件,因为null中的第一个空,一,它会得到满足,因为sizeof(null)仍然返回 0 即使它给了你不可数的警告。

sizeof was never really necessary there. sizeof在那里从来没有真正必要。 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();
}

You can just check, before passing the variable, if it's an array or not:您可以在传递变量之前检查它是否是数组:

if(is_array($this->config)){
   if(sizeof($this.config) < 1) {
       // code here
   }
}

For starters don't use sizeof , but count ;) cheap improvement - always one opcode less.对于初学者来说,不要使用sizeof ,而是使用count ;) 廉价的改进 - 总是少一个操作码。

Secondly, make sure you pass an array, not null or whatever you have there, eg.:其次,确保传递一个数组,而不是 null 或任何你有的,例如:

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

To fix this properly you should never allow this property to be null anyway.要正确解决此问题,无论如何您都不应允许此属性为null If you're after some lazy loading check if the variable is null or is not an array, eg:如果您在延迟加载后检查变量是 null 还是不是数组,例如:

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;
}

These days you can use the null coalescing operator (??) to help in this scenario;这些天来,您可以使用null 合并运算符(??) 在这种情况下提供帮助; ie by using it to take the given value if it's not null, or taken an empty array when the given value is null.即,如果它不是 null,则使用它来获取给定值,或者当给定值为 null 时采用空数组。

So your code: if (sizeof($this->config) < 1) { ...would become: if (sizeof($this->config?? []) < 1) { .所以你的代码: if (sizeof($this->config) < 1) { ...会变成: if (sizeof($this->config?? []) < 1) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:sizeof():参数必须是在事件生成器上实现 Countable 的数组或对象 - Warning: sizeof(): Parameter must be an array or an object that implements Countable on event maker 警告:sizeof():参数必须是一个数组或一个实现 Countable 的对象 - Warning: sizeof(): Parameter must be an array or an object that implements Countable 如何修复 PHP 7.2 警告:count():参数必须是一个数组或实现 Countable in errors.php 的数组或 object? - How to fix PHP 7.2 Warning: count(): Parameter must be an array or an object that implements Countable in errors.php? PHP 7.2: count(): 参数必须是数组或实现Countable的object - PHP 7.2: count(): Parameter must be an array or an object that implements Countable sizeof():参数必须是一个数组或者一个实现了Countable的对象 - sizeof(): Parameter must be an array or an object that implements Countable PHP 警告:参数必须是使用 xampp 实现可数的数组或 object - PHP Warning: Parameter must be an array or an object that implements Countable using xampp PHP 错误 - 警告:count():参数必须是实现 Countable 的数组或对象 - PHP Error - Warning: count(): Parameter must be an array or an object that implements Countable phpdoc:PHP警告:count():参数必须是实现Countable的数组或对象 - phpdoc: PHP Warning: count(): Parameter must be an array or an object that implements Countable PHP错误:警告:count():参数必须是实现Countable的数组或对象 - PHP error: Warning: count(): Parameter must be an array or an object that implements Countable PHP 7.3 警告:count():参数必须是实现 Countable 的数组或对象 - PHP 7.3 Warning: count(): Parameter must be an array or an object that implements Countable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM