json_encode类时丢失私有属性和受保护属性处理方法
2021-05-13 19:30
标签:protected php data code ons serial return UNC foreach 官网文档地址: https://php.net/manual/en/class.jsonserializable.php json_encode类时丢失私有属性和受保护属性处理方法 标签:protected php data code ons serial return UNC foreach 原文地址:https://www.cnblogs.com/junyi-bk/p/11994417.htmlphp
header("Content-Type: text/html;charset=utf-8");
class a implements JsonSerializable{
public $b1 = 1;
private $b2 = 2;
protected $b3 = 3;
// 实现的抽象类方法,指定需要被序列化JSON的数据
public function jsonSerialize() {
$data = [];
foreach ($this as $key=>$val){
$data[$key] = $val;
}
return $data;
}
}
$json = json_encode(new a);
echo $json;
header("Content-Type: text/html;charset=utf-8");
class a implements JsonSerializable{
public $b1 = 1;
private $b2 = 2;
protected $b3 = 3;
// 实现的抽象类方法,指定需要被序列化JSON的数据
public function jsonSerialize() {
$data = [];
foreach ($this as $key=>$val){
$data[$key] = $val;
}
return $data;
}
}
$json = json_encode(new a);
echo $json;
下一篇:ajax练习
文章标题:json_encode类时丢失私有属性和受保护属性处理方法
文章链接:http://soscw.com/index.php/essay/85284.html