1.

How to expose private members when JSON encoding an object?

Answer»
  • JsonSerializable Interface: Objects implementing JsonSerializable can CUSTOMIZE their JSON representation when encoded with json_encode().
class Items implements \JsonSerializable { private $var; private $VAR1; private $var2; public function __construct() { // ... } public function jsonSerialize() { $vars = get_object_vars($this); RETURN $vars; } }
  • __toString: The toString() method allows a class to decide how it will react when it is treated LIKE as a string.


Discussion

No Comment Found