简体   繁体   English

计算不可数对象PHP 7.2

[英]Count for non countable object PHP 7.2

I have been using PHP 5.4 until now in one of my old projects. 到目前为止,我一直在一个老项目中使用PHP 5.4

Recently I have decided to upgrade to PHP 7.2 最近,我决定升级到PHP 7.2

I have around 800 files in my project and old developer team used count() hundreds of time in the project. 我的项目中大约有800个文件,旧的开发人员团队在项目中使用了count()数百次。

In PHP 5.4 even if the variable was not countable it worked as expected but in PHP 7.2 it throws an error. PHP 5.4即使该变量不是可计数的,它也可以按预期工作,但在PHP 7.2它将引发错误。

I could go into each file and change the function but that could take a lot of time and the risk of not changing a couple of function here and there. 我可以进入每个文件并更改功能,但这可能会花费很多时间,并且有可能在此处和此处不更改几个功能。

Can I extend count() function and write code to make it work as it used to in PHP 5.4 ? 我可以扩展count()函数并编写代码以使其像PHP 5.4吗?

Is there any other solution to this problem? 还有其他解决方案吗?

I am replacing all if conditions with count($var) functions to is_array($var) && count($var) for now. 我现在将所有if条件用count($var)函数替换为is_array($var) && count($var)

Thank you 谢谢

In other languages like Python you can use decorators to intercept an existing method without modifying the original method. 在其他语言(如Python)中,您可以使用装饰器来拦截现有方法,而无需修改原始方法。

However PHP does not support decorators so you will need to manually change every location of that call. 但是,PHP不支持装饰器,因此您需要手动更改该调用的每个位置。

The difference between php 5.4 and php 7+ on that aspect is not that count changed it's behavior, is that php now turned errors into catchable exceptions. php 5.4和php 7+之间在这方面的区别不是计数改变了它的行为,而是php现在将错误变成了可捕获的异常。 Your code was failing silently on production with error_repoting turned off. 您的代码在生产中静默失败,并关闭了error_repoting。 That is extremely dangerous for the business, as the consistency of your app is unreliable, you should change that asap. 这对于企业来说是极其危险的,因为您的应用程序的一致性不可靠,因此您应尽快进行更改。

Having that said. 话虽如此。 Since all errors cannot be turned off anymore by setting your error_reporting to 0 (thats because now they are exceptions rather than errors, and that's really nice, php is no more a super unreliable language). 由于无法通过将error_reporting设置为0来关闭所有错误(那是因为现在它们是异常而不是错误,这真的很好,php不再是一种超级不可靠的语言)。 You have to catch those exeptions, which are ErrorException. 您必须抓住那些例外,即ErrorException。

Go to the index file of your application and add a try-catch block and swipe the dirty down the carpet again. 转到应用程序的索引文件,并添加一个try-catch块,然后再次将脏物向下滑动地毯。

try {
   //super buggy app bootstraping
} catch (ErrorException $ex) {
    error_log($ex);
}

Well, i'd rather fix the counts anyway, they are toxic. 好吧,无论如何我宁愿修正计数,它们是有毒的。

Note: is_array($var) && count($var) is not a goo idea, since arrays is not the only countable thing on php, ((is_array($var) || $var instanceof countable) && count($var)) would be a better, but not perfect solution. 注意:is_array($ var)&& count($ var)并不是一个好主意,因为数组不是php上唯一可计数的东西,((is_array($ var)|| $ var instanceof countable)&& count($ var) )将是一个更好但不是完美的解决方案。 Also note a bug in php on any version - count(false) - returns 1 还要注意任何版本的php中的错误-count(false)-返回1

You can use runkit to overload the count() function with your own implementation: 您可以使用runkit在自己的实现中重载count()函数:

// to override internal functions, this system INI value needs truthy
assert((bool)ini_get('runkit.internal_override'));

// now you can define your own implementation
runkit_function_redefine('count', function ($var) {
    return ((is_array($var) || $var instanceof \Countable) ? count($var) : 0);
});

However, please, consider using this only as a temporary stop gap to ease the pain of 5.4 to 7.2. 但是,请考虑将其仅用作临时停止间隙以减轻5.4至7.2的痛苦。 Because this method is opaque, it'll become a maintenance liability at some point, a liability you probably don't want to linger. 由于此方法是不透明的,因此在某些时候它会成为维护责任,您可能不想持续这种责任。

Additionally, note that your conversion of is_array($v) && count($v) does not handle all countable things: ie, \\Generator and \\Traversable . 另外,请注意,您对is_array($v) && count($v)不能处理所有可计数的事物:即\\Generator\\Traversable The runkit override example above does handle them, though. 不过,上面的runkit覆盖示例确实可以处理它们。

Fix your code rather, and learn to type . 修正您的代码,然后学习type Make sure your count method returns something countable 确保您的count方法返回可计数的值

暂无
暂无

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

相关问题 PHP 7.2: count(): 参数必须是数组或实现Countable的object - PHP 7.2: count(): Parameter must be an array or an object that implements Countable PHP 7.2 警告count():参数必须是数组或实现Countable的object - PHP 7.2 Warning count(): 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? 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 -> 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 PHP 7.2的可数问题 - Countable issue for PHP 7.2 PHP:计算没有Countable的ArrayAccess对象 - PHP: Count an ArrayAccess object without Countable PHP可数count()或-> count() - PHP Countable count() or ->count() 警告:sizeof():参数必须是一个数组或实现 Countable php7.2 的 object - Warning: sizeof(): Parameter must be an array or an object that implements Countable php7.2 PHP警告:count():参数必须是数组或实现Countable的对象? - PHP Warning: count(): Parameter must be an array or an object that implements Countable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM