NotionCommotion Posted May 30, 2018 Share Posted May 30, 2018 I have an entity object which contains other entity objects which in turn contain other entity objects. Of of these entity object implements ArrayAccess and contains a collection of entity objects. All the properties in these objects are protected. To get the applicable data, all of these objects implement JsonSerializable and include a jsonSerialize() method which cascade and to my amazement provide my exact desired stdClass which I convert to JSON and send to the client. But now I need a different view of the object. By different view, I mean a different sub-set of properties and potentially calling them different names. How should this be accomplished? I can obviously manually iterate over the object and manipulate it, but I am sure that doing so is the wrong approach. Or maybe extend all of the objects and override jsonSerialize() in each? This seems like a lot of work. Ideally, I would have some public method on the top object like $topObject->getView1() which would return one stdClass, and $topObject->getView2() which would return another. But I don't understand how this cascading of nested objects into their parents would work as I can only have one jsonSerialize() in each class. Maybe passing jsonSerialize() a value somehow gets passed to its children objects? Sounds prone to error, but maybe not. Any thoughts? Thank you Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.