Explanation on php json_encode not supporting object private attributes

jacklove
Release: 2023-03-30 19:40:01
Original
1833 people have browsed it

This article introduces the solution for php json_encode that does not support the private properties of objects. json_encode can convert the object into json format, and use json_decode to restore it to the object.

But if the object contains private attributes, the private attributes will be lost after json_encode is executed.

Example: json_encode loses object private attributes

<?php// 用户类class user{

    public $id = 1;    public $name = &#39;fdipzone&#39;;    public $profession = &#39;programmer&#39;;    private $age = 18;

}// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json);
var_dump($oUser);?>
Copy after login

Output:

{"id":1,"name":"fdipzone","profession":"programmer"}object(stdClass)[2]  public &#39;id&#39; => int 1
  public &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  public &#39;profession&#39; => string &#39;programmer&#39; (length=10)
Copy after login

After executing json_encode, the private attribute age is lost.

Solution to the loss of private attributes of the object after json_encode

The modified code is as follows:

<?php// 用户类class user implements JsonSerializable{

    public $id = 1;    public $name = &#39;fdipzone&#39;;    public $profession = &#39;programmer&#39;;    private $age = 18;    // 实现的抽象类方法,指定需要被序列化JSON的数据
    public function jsonSerialize() {
        $data = [];        foreach ($this as $key=>$val){            if ($val !== null) $data[$key] = $val;
        }        return $data;
    }

}// 对象$oUser = new User;// json_encode$json = json_encode($oUser);echo $json;// json_decode$oUser = json_decode($json);
var_dump($oUser);?>
Copy after login

Output:

{"id":1,"name":"fdipzone","profession":"programmer","age":18}object(stdClass)[2]  public &#39;id&#39; => int 1
  public &#39;name&#39; => string &#39;fdipzone&#39; (length=8)  public &#39;profession&#39; => string &#39;programmer&#39; (length=10)  public &#39;age&#39; => int 18
Copy after login

After specifying the data that needs to be serialized into JSON, json_encode can read the private attribute age.

This article explains the relevant knowledge that php json_encode does not support object private attributes. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Explanation on PHP generating unique RequestID class related content

MySQL View database table capacity size

Detailed explanation of a singleton mode Mysql operation class encapsulated by PHP

The above is the detailed content of Explanation on php json_encode not supporting object private attributes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!