php - lumen array_key_exists() depreciated, then how to use the alternative like isset() or property_exists() to array?

Solution:

can work with objects but that behavior is deprecated in php 7.4.0 and removed in php 8 :

Note:

For backward compatibility reasons, array_key_exists() will also return true if key is a property defined within an object given as array. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.

To check whether a property exists in an object, property_exists() should be used.

So, you can change your code to :

// Take note that the order of parameters is inverted from the array_key_exists() function
//                    |       |
//                    V       V
if(property_exists($object, $key))
{
    $keyData = "true";
}

Answer

Solution:

While the above answer solves the issue for some cases, it doesn't when you have protected/private properties.

I used array_key_exists to check if the property was private/protected, where the last ones would be marked with an asterisk by the function and return false, cos the name mismatches.

I solved this like this:

array_key_exists($key, (array)$obj);

So type casting the object should solve it even in php8. I think in php 7 and earlier this was being done under the hood. In 8 they removed it, maybe performance issues??

Answer

Solution:

A lot of people recommending property_exists($object, $key) but it is for objects or class properties not for an array. It will return an error if you check array.

First parameter must either be an object or the name of an existing class

Source