Using json_encode on objects in PHP (regardless of scope)

All the properties of your object are private. aka… not available outside their class’s scope. Solution for PHP >= 5.4 Use the new JsonSerializable Interface to provide your own json representation to be used by json_encode class Thing implements JsonSerializable { … public function jsonSerialize() { return [ ‘something’ => $this->something, ‘protected_something’ => $this->get_protected_something(), ‘private_something’ … Read more