Is there any way I can set up PHP objects so that when I try to convert them to JSON, all of their protected properties will be shown?
I have read other answers suggesting I add a toJson() function to the object, but that may not really help me a great lot. In most cases, I have an array of objects and I perform the encode on the array itself.
$array = [
$object1, $object2, $object3, 5, 'string', $object4
];
return json_encode($array);
Yes, I can loop through this array and call toJson() on every element that has such method, but that just doesn't seem right. Is there a way I can use magic methods to achieve this?
serialise_to_json([$obj1, ..]), whereserialise_to_jsoncontains half a ton of code. I'm sure such libraries already exist.toJson()to my objects which would get their properties internally without the need of reflectiontoJson()is not possible for all kinds of objects since you can't modify internal classes. An external serializer could work for all kinds of objects