Why can't I call property_exists on stdClass? Why can't I call property_exists on stdClass? php php

Why can't I call property_exists on stdClass?


Try:

if( property_exists($madeUpObject, 'madeUpProperty')) {

Specifying the class name (instead of the object like I have done) means in the stdClass definition, you'd need the property to be defined.

You can see from this demo that it prints:

abcexists 


Because stdClass does not have any properties. You need to pass in $madeUpObject:

property_exists($madeUpObject, 'madeUpProperty');

The function's prototype is as follows:

bool property_exists ( mixed $class, string $property )

The $class parameter must be the "class name or an object of the class". The $property must be the name of the property.


Unless you're concerned about NULL values, you can keep it simple with isset.