Type-hinting for stdClass properties?

Viewed 694

Is there a standard way to type-hint the properties that exist on a stdClass?

For example, I use some API and get a JSON response. I then parse it with json_decode, but I want the IDE to be aware of what properties this stdClass object has.

I tried doing it like this:

$obj = json_decode($jsonResponse);
/** @var $obj \stdClass */
/** @property String $obj->prop */

But PhpStorm still doesn't recognise that $obj has the property prop.

Is there anyway to get this working?

1 Answers

You can create a class that will inherit stdClass and will describe properties. Response object might be converted to it. For example:

/**
 * @property string $prop
 */
class SomeClass extends stdClass
{
}

$obj = json_decode($jsonResponse);
$obj = (SomeClass)$obj;
Related