json_encode类时丢失私有属性和受保护属性处理方法
2021-05-13 19:30
阅读:470
标签:protected php data code ons serial return UNC foreach
php 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;
官网文档地址: https://php.net/manual/en/class.jsonserializable.php
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;
json_encode类时丢失私有属性和受保护属性处理方法
标签:protected php data code ons serial return UNC foreach
原文地址:https://www.cnblogs.com/junyi-bk/p/11994417.html
下一篇:ajax练习
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:json_encode类时丢失私有属性和受保护属性处理方法
文章链接:http://soscw.com/index.php/essay/85284.html
文章标题:json_encode类时丢失私有属性和受保护属性处理方法
文章链接:http://soscw.com/index.php/essay/85284.html
评论
亲,登录后才可以留言!