By using this method the result will be a json with value for properties initialized according to each method.

Things to note1.if you are using the reading properties approach the code cant' work outside of the class for private properties, because they are nto visible, you have to put it the code directly to public function jsonSerialize() {} in order to work.2.For both approaches has method seems not to be case sensitive else you are going to have problems, i strongly suggest to pay attention in naming convention between property names and method names idPort =>setIdPort() =>getIdPort() (i know my class is done not that way).3.both approaches since you call method without knowing the arguments you need to pay attention in arguments for getter methods where applicable, check my example public function getPortLanguageAtIndex($index=false){ if(!$index){ $index=0; } return $this->portLanguages[$index]; }that's why i have set it to optional argument. If you don't do that php will complain about missing argument parameter in method XXX.

You can't throw exceptions in here. If you do, you'll get an exception with the message "Failed calling FooClass::jsonSerialize()" and the stacktrace will start at where you called the json_encode() method.