Let's say I have an instance of a class that contains dozens of properties. This class cannot be modified or extended.
I now need to pass some data from this instance to a function. The function just needs a few of these properties and it cannot directly use an instance of a class (it expects an associative array) and also needs some data not present in the instance. So I need to convert the instance to an associative array.
For example I need to convert this:
class Person {
public $id = 123;
public $firstName = "John";
public $lastName = "Something";
public $address;
public $city;
public $zipCode;
// etc.
}
to this:
array(
'id' => 123,
'name' => 'John Something'
);
My question is: is there any known OOP pattern to handle this kind of conversion? I know I could write a simple function to convert from one format to another but I'd like to know what is the "proper" way to do it, basically use a known pattern if possible.