I want to set attribute for a stdClass object in a single statement. I don\'t have any idea about it. I know the following things
$obj = new stdClass;
$
Though Ja͢ck gives a good answer, it is important to stress that the PHP interpreter itself has a method for describing how to properly represent an object or variable:
php > $someObject = new stdClass();
php > $someObject->name = 'Ethan';
php > var_export($someObject);
stdClass::__set_state(array(
'name' => 'Ethan',
))
Interestingly, using stdClass::__set_state
fails to create a stdClass object, thus displaying it as such is likely a bug in var_export(). However, it does illustrate that there is no straightforward method to create the stdClass object with attributes set at the time of object creation.