1

I really need a way to cast my entity to an Array. I know can do it manually but doing to on each entity is painfull, more working with relationship will not be easy.

How do you do? Have you implemented anything like that?

JohnT
  • 947
  • 2
  • 16
  • 30

1 Answers1

2

You can use get_class_methods to find the getters:

function toArray($object)
{
    $result = array();
    $methods = get_class_methods($object);
    foreach($methods as $method) {
        if ('get' == substr($method, 0, 3)) {
            $result[substr($method, 3)] = $object->$method();
        }
    }
    return $result;
}
Maxence
  • 12,184
  • 4
  • 48
  • 66