OOP question

Published 10 months ago by bart

Hey guys,

I'm using the Affilinet PHP SDK that returns product data for a given shop. The data I get in return is an object with private properties and lots of getters.

What I want to store this raw object in my database for further manipulation later on. But using json_encode() does not work here, due to the private scope of the properties. I can't add the implementation of \JsonSerializable because it's a third party class.

Do you have any idea how to store the plain object JSON? Thanks a lot!


There are two ways out of the top of my head

  1. create array by manually calling all necessary getters.
  2. Use ReflectionClass to extract all values for properties without getters. (You can even iterate over all properties)

When array is ready you can encode it.

10 months ago (58,100 XP)

Hey @revati,

thanks for the answer. Well, creating an array and setting all keys and values manually using the corresponding getter is possible. But I hope that there is a more elegant way to solve this.

I already played around with ReflectionClass but is it possible to get the private values from the original class using this way? That would do the trick!

10 months ago (58,100 XP)

Okay, just for your information: it is possible accessing private values by using the setAccessible() method of the reflected property before. That does the trick here.

Please sign in or create an account to participate in this conversation.