简体   繁体   English

PHP 7.2的可数问题

[英]Countable issue for PHP 7.2

I have a client that's been relying on a WP plugin for years that was abandoned by the author a few years ago. 我有一个客户多年来一直依赖WP插件,而该插件几年前就被作者放弃了。 I've been able to make some fixes to make it work with an upgrade to PHP 7.2, but this one puzzles me. 我已经能够进行一些修复以使其能够升级到PHP 7.2,但这使我感到困惑。 The more I research it, I feel like the less I understand about how to fix it. 我研究得越多,就越不了解如何解决它。

Warning below: 以下警告:

Warning: count(): Parameter must be an array or an object that implements Countable in /app/public/wp-content/plugins/ozh-admin-drop-down-menu/inc/core.php on line 311 ( first line below is 311 ) 警告:count():参数必须是第311行( 第一行 /app/public/wp-content/plugins/ozh-admin-drop-down-menu/inc/core.php中实现Countable的数组或对象) 下面是311

if (!count($wp_ozh_adminmenu)) {
    $wp_ozh_adminmenu = (array)get_option('ozh_adminmenu');
    unset($wp_ozh_adminmenu[0]);
}

Before PHP 7.2 when passing a non-array to count() , it returned 1. 在PHP 7.2之前,将非数组传递给count() ,它返回1。
Now, as you can see in the breaking changes , count() emits a warning when you give it a non-array. 现在,您可以在重大更改中看到,当您给它一个非数组时, count()会发出警告。

A simple workaround is to check if it's an array before using it. 一个简单的解决方法是在使用它之前检查它是否为数组。 It may require a bit more checks, if you want to allow arrays and string for example. 例如,如果要允许数组和字符串,则可能需要更多检查。

<?php
if (!is_array($wp_ozh_adminmenu) || !count($wp_ozh_adminmenu)) {
    $wp_ozh_adminmenu = (array)get_option('ozh_adminmenu');
    unset($wp_ozh_adminmenu[0]);
}

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

相关问题 计算不可数对象PHP 7.2 - Count for non countable object PHP 7.2 PHP 7.2: count(): 参数必须是数组或实现Countable的object - PHP 7.2: count(): Parameter must be an array or an object that implements Countable PHP 7.2和laravel的问题 - issue with php 7.2 and laravel 如何修复 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? yii2 gridview count():参数必须是实现Countable php 7.2的数组或对象 - yii2 gridview count(): Parameter must be an array or an object that implements Countable php 7.2 错误 php 7.1 -&gt; 7.2:count(): Parameter must be an array or an object that implement Countable in concrete5 addon - Error php 7.1 -> 7.2 :count(): Parameter must be an array or an object that implements Countable in concrete5 addon 警告:sizeof():参数必须是一个数组或实现 Countable php7.2 的 object - Warning: sizeof(): Parameter must be an array or an object that implements Countable php7.2 "Swoole php 模块:未定义符号:spl_ce_Countable 问题" - Swoole php module : undefined symbol: spl_ce_Countable issue php 7.2的Windows zmq dll问题 - windows zmq dll issue for php 7.2 安装php7.2-mcrypt的问题 - Issue in installing php7.2-mcrypt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM