phpphp-8php-7.4

change PHP compatibility from 7.4 to 8.0


I have a php script and need to make this code line compatible to php8.0:

if ((is_array($key) && is_array($value)) || (strlen($key) > 0 && strlen($value) > 0)) {

I am not a programmer/coder. What I found out is, that the second part (strlen($key) > 0 && strlen($value) > 0) makes the error. Because I get this error message in the application:

Response: {"result":null,"error":{"code":0,"message":"strlen (): Argument #1 ($string) must be of type string, jtl\Connector\Model\Identity given","data":"Exception 'TypeError' (Code: 0) with message 'strlen(): Argument #1 ($string) must be of type string, jtl\Connector\Model\Identity given' in C:\xampp\htdocs\mod2060\jtlconnector\vendor\ jtl\connector\src\Core\Database\Mysql.php:567 "},"jtlrpc":"2.0","id":"unknown"} Exception bei category.push: strlen(): Argument #1 ($string) must be of type string, jtl\Connector\Model\Identity given

I just changed PHP Version from 7.45 to 8.00 and get the error. If I go back to PHP 7.45 it works fine again.


Solution

  • This error is being thrown because either $key or $value is an object and not a string - the error message says it is an object of type 'jtl\Connector\Model\Identity'. So you would either need to modify that library and create a __toString magic method function in it for that class, which defines how to convert that object to a string, or you might want to do an is_object check instead of (or before) using strlen.

    1 https://www.php.net/manual/en/language.oop5.magic.php#object.tostring