Saturday, January 5, 2013

PHP:json_encode private properties

json_encode( $obj ) works with public properties. In case you have private properties, you can implement jsonSerialize()::JsonSerializable. json_encode for objects of this class will create json with properties or value you specify in jsonSerialize. The output of jsonSerialize should either be array (normal or associative). The below code has a provision to filter attributes added in the $exclude_properties_to_json_arr var.Works with PHP 5.4 and above.

No comments: