Loss of private property and the protection of property class treatment by the time json_encode

<?php
header("Content-Type: text/html;charset=utf-8");

class a implements JsonSerializable{

    public $b1 = 1;
    private $b2 = 2;
    protected $b3 = 3;

    // abstract class implemented method, JSON Specifies the data to be serialized 
    public  function jsonSerialize () {
         $ Data = [];
         the foreach ( $ the this  AS  $ Key => $ Val ) {
            $ Data [ $ Key ] = $ Val ;
        }
        return $data;
    }
}

$json = json_encode(new a);
echo $json;

 

Document official website address:  https://php.net/manual/en/class.jsonserializable.php

? <PHP 
header ( "the Content-the Type: text / HTML; charset = UTF-. 8" );
class A the implements JsonSerializable { public $ B1 = . 1 ; Private $ B2 = 2 ; protected $ B3 = . 3 ; // implemented abstractions class method, designated to be serialized JSON the number of data public function jsonSerialize () { $ Data = []; the foreach ( $ the this AS $ Key => $ Val ) { $ Data [










$key] = $val;
}
return $data;
}
}

$json = json_encode(new a);
echo $json;

Guess you like

Origin www.cnblogs.com/junyi-bk/p/11994417.html