I have models with attributes which contains objects of classes with protected properties. I want to output the model in json including the protected properties.
I am working with these constraints:
- Said classes are from a third party library which I cannot edit, so I cannot add in a JsonSerializable interface.
- I am using the attributes as arguments for the library, so I don't want to extend them and use an accessor. If I were to extend it I would have to convert it back for every time I use it as an argument for the library. So I am trying to avoid that.
-
class MyModel extends Model {}
class Pen {
protected $colour = 'red';
}
class Library {
public static function pineapple(Pen $pen) {
}
}
$myModel->pen = (new Pen);
// I need to use them for a third party library so I should not change the class
Library::pineapple($myModel->pen);
// In controller
return response()->json([
'data' => $myModel
]);
I want the output to contain {pen: { colour: 'red' }}