4

I have a working php application and it is running fine on php 7.0 version. But when I upgrade a php version to 7.2. I am getting this error:

count(): Parameter must be an array or an object that implements Countable

I am getting errors on code where I am comparing my data with count function. For example this is my code:

$keytest = KeyUser::where('key', '=', $key)->first();
 if (count($keytest) == 1) {
   //logic ... 
  }

I am using laravel where I am running a query and counting it if it is equal to 1 then logic should work.

So my problem is I have written this kind of logic on many controllers and if I have to change everything one by one it could become nightmare. So is there any way where I can write a global function to make count work as it was working in php older version. What can be the easiest fix.

  • That's logical. First returns objects. You should use get() method in order to retrieve an array – Sakezzz Jun 03 '18 at 14:19
  • try doing var_dump($keytest) and check what is the type returned for $keytest – 5eeker Jun 03 '18 at 14:25
  • I can do that but I need to change all of the code my question is can we write any global function to make it workable my application is very big. –  Jun 03 '18 at 14:27
  • https://github.com/yiisoft/yii/issues/4167 php made changes to count function I need a solution to make it work. –  Jun 03 '18 at 14:29
  • It is actually provable: https://3v4l.org/MFVQC -- very interesting to be honest. I think you could write your own `count()` function that checks for `is_object()` and returns `1` in this case, not throwing an error. And after this check you perform the old `count()` call. But I have no idea if it is possible to override built-in functions of the language itself, so you might need to update all references to the function. – Namoshek Jun 03 '18 at 14:33
  • Oh, I forgot: It doesn't seem a good idea to do so, though. There will some wtf moments in future for sure, if you do this (either by you or other people that pick up your code). Maybe run a count over your code to find out how often you used `count()` (the wrong way) - maybe it is worth fixing the occurences in a different way. – Namoshek Jun 03 '18 at 14:36
  • Follow latest standards. Regarding behavior prior to PHP 7.2 all you expect from your code is to check if result (`$keytest`) is wether or not `null`. You should use `if (!is_null($keytest)) {/* code */}`. – Tpojka Jun 03 '18 at 16:53

3 Answers3

8

This problem can be handle using disable error handling. Please refer this link for solution: Laravel not compatiable with php 7.2

Here I found a solution to your problem simply write this code inside your controller or if you want to make it work for whole application write this code in route.php:

 //app/Http/routes.php

 if (version_compare(PHP_VERSION, '7.2.0', '>=')) {
// Ignores notices and reports all other kinds... and warnings
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
// error_reporting(E_ALL ^ E_WARNING); // Maybe this is enough
}

I know this is not the best solution but it can be a good hack.

Nilay Singh
  • 2,201
  • 6
  • 31
  • 61
  • Thanks for this solution it worked all of my warning is gone. –  Jun 03 '18 at 15:15
  • 1
    Beware! Handling the problem by disabling the error reporting is just a FISHY WORKAROUND rather than becoming a SOLUTION. Please stop misguiding the newcomers who need to understand the real issue behind. Workarounds are not solutions! The correct solution is using well written libraries / frameworks which does not explodes on minor version upgrades, at least adaptable in a reasonable time window. Passing arbitrary variables/types to the native `count()` function was always a design problem, even before the release of PHP 7 but brillant maintainers ignored it until the real warning arrives. – edigu Mar 01 '19 at 21:25
2

It's solved when you change your code:

$keytest = KeyUser::where('key', '=', $key)->first();
if ($keytest) {
   //logic ... 
}
1

Try to use this Instead of "count" you can use "empty" function to check the contents. Eg: Instead of:

if ( count( $data ) ) 

Use:

if ( ! empty( $data ) )
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ephra
  • 111
  • 1
  • 10