OOP question

Published 5 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!

revati

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.

bart
bart
5 months ago (56,720 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!

bart
bart
5 months ago (56,720 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.

Sign In or create a forum account to participate in this discussion.