PHP Json_encode does not support the solution of object private properties __js

Source: Internet
Author: User

This article describes that the PHP Json_encode does not support the solution of object private properties, Json_encode can convert objects to JSON format and use Json_decode to revert to objects.

However, if the object contains private properties, the private property is lost when the Json_encode is executed.

Example: Json_encode lost object Private Property

<?php
//user class
user{public

    $id = 1;
    Public $name = ' fdipzone ';
    Public $profession = ' Programmer ';
    Private $age =;

}

Object
$oUser = new User;

Json_encode
$json = Json_encode ($oUser);
echo $json;

Json_decode
$oUser = Json_decode ($json);
Var_dump ($oUser);
? >

Output:

{"id": 1, "name": "Fdipzone", "Profession": "Programmer"}

Object (StdClass) [2] public
  ' ID ' => int 1 public
  ' name ' => string ' Fdipzone ' (length=8) public
  ' Profession ' => string ' Programmer ' (length=10)

The private property age was lost after the Json_encode was executed.

Json_encode object's private property loss resolution

We can modify the class so that it implements the interface method Jsonserialize of the Jsonserializable abstract class, specifying the data that needs to be serialized into JSON.

About Jsonserializable::jsonserialize Method can refer to official website: http://php.net/manual/zh/jsonserializable.jsonserialize.php

The modified code is as follows:

<?php
//User classes Class
user implements jsonserializable{public

    $id = 1;
    Public $name = ' fdipzone ';
    Public $profession = ' Programmer ';
    Private $age =;

    Implements an abstract class method that specifies the data public
    function jsonserialize () {
        $data = [] that needs to be serialized JSON;
        foreach ($this as $key => $val) {
            if ($val!== null) $data [$key] = $val;
        }
        return $data;
    }

Object
$oUser = new User;

Json_encode
$json = Json_encode ($oUser);
echo $json;

Json_decode
$oUser = Json_decode ($json);
Var_dump ($oUser);
? >

Output:

{"id": 1, "name": "Fdipzone", "Profession": "Programmer", "Age":

Object (StdClass) [2] public
  ' ID ' => int 1 public
  ' name ' => string ' Fdipzone ' (length=8) public
  ' Profession ' => string ' Programmer ' (length=10) public
  ' age ' => int 18

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.