开发者

How to instantiate stdClass in place

开发者 https://www.devze.com 2022-12-10 06:30 出处:网络
Is it it possible to do this in php? Javascript code开发者_JAVA百科: var a = {name: \"john\", age: 13}; //a.name = \"john\"; a.age = 13

Is it it possible to do this in php?

Javascript code开发者_JAVA百科:

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'


You can also do:

$a = new stdClass;
$a->name = 'john';
$a->age = 13;


Another way:

$text = '{"name": "john", "age": 13}';
$obj = json_decode($text);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号