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 ?
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 ?
Try using the associative array syntax, and casting to object
:
$a = (object)array('name' => 'john', 'age' => 13);
echo $a->name; // 'john'
stdClass
use to work or something? I have it in my head that it did work once upon a time. –
Friseur You can also do:
$a = new stdClass;
$a->name = 'john';
$a->age = 13;
Another way:
$text = '{"name": "john", "age": 13}';
$obj = json_decode($text);
© 2022 - 2024 — McMap. All rights reserved.