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";
}
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??
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
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.