Is it it possible to do this in php?
Javascript code:
var a = {name: "john", age: 13}; //a.name = "john"; a.age = 13
Instantiate the stdClass variable on the fly ?
The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.
The stdClass is a generic empty class used to cast the other type values to the object. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. The stdClass is not the base class for objects in PHP.
Try using the associative array syntax, and casting to object
:
$a = (object)array('name' => 'john', 'age' => 13);
echo $a->name; // 'john'
You can also do:
$a = new stdClass;
$a->name = 'john';
$a->age = 13;
Another way:
$text = '{"name": "john", "age": 13}';
$obj = json_decode($text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With