如何在php中按名称设置类成员?

时间:2013-04-24 22:07:34

标签: php

我想从几种不同的格式反序列化字符串。第一种格式是json。

$myObj = new myObject();
$unserialized = json_decode($input, true);
foreach ($unserialized as $member_name => $value){
    $myObj[$member_name] = $value; // <= How do I do what this is trying to do?
}

1 个答案:

答案 0 :(得分:2)

这很简单:

$myObj->$member_name = $value;