Laravel 5.3 errors with PHP 7.2

Laravel 5.3 ErrorException in Builder.php when switching to PHP 7.2

December 11, 2017
In category Makeitwork

I always like to run the software latest versions, because the software gets more secure, fast and bug free. In case of Laravel, there is a process and some effort required to update to the latest version. When I updated from PHP 7.1.11 to PHP 7.2, I noticed a strange error when tried to navigate to this blogs’ admin section.

Laravel 5.3 error with PHP 7.2

PHP 7.2 has a backward incompatibility for count function, that will throw a notice when a non array variable passes to count function

From PHP 7.2 migration # Backward incompatible changes

Warn when counting non-countable types

An E_WARNING will now be emitted when attempting to count() non-countable types (this includes the sizeof() alias function).

This was reported at #19380 issue and is fixed with this PR for Laravel 5.4 and 5.5, but not for 5.3. If we check at the PR commit, we’ll see that the count($query->wheres) is changed with a ternary check and it counts its’ elements only if it’s not NULL:

-    $originalWhereCount = count($query->wheres);
+    $originalWhereCount = is_null($query->wheres) ? 0 : count($query->wheres);

I opened Illuminate/Database/Eloguent/Builder.php in my local application, and I changed the lines 1231 and 1277:

// Line 1231
-    $originalWhereCount = count($query->wheres);
+    $originalWhereCount = is_null($query->wheres) ? 0 : count($query->wheres);
// Line 1277
-    return count($query->wheres) > $originalWhereCount;
+    return (is_null($query->wheres) ? 0 : count($query->wheres)) > $originalWhereCount;

Now it’s working as it should, but there might be some more errors like this one. I know that the best thing to do here is take some time and update the blog to the latest Laravel version and I will do it. But until I do, this is a small fix to get things working with the latest PHP version.

0 0

comments powered by Disqus