atoms atoms - 9 months ago 56
PHP Question

Retreiving PHP property name as string

I have some JSON, if I

the output looks like:

var_dump( $oJSON->{$oQ->sQuestionName} );

public 'Lease' => boolean true

I would like to retrieve 'Lease' as a string.

I've tried casting it:


but it returns an

E_RECOVERABLE_ERROR Error: Object of class stdClass could not be converted to string

The following works, however I feel there must be a better way?


N.B. I can't use to use the following due to compatability issues


Any ideas?

Answer Source

Use ReflectionClass::getProperties() to get a list of the object's properties.

$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);