Edge
Edge

Reputation: 911

Symfony2 - How to access entity methods dynamically?

I want to access the Symfony2 entity methods dynamically by calling it's object. For Instance:

$entityObj = new Products();

// Generic Table Processor to process the table data    
private function tableProcessor($entityObject){

    // how can I get all the Entity methods inside the Products Entity????

    // e.g; $entityObject.getMethods();   // should return all the methods?

    return $entityObject;
}

If sorted out! I'm sure this procedure gonna help me a lot in writing less code, which otherwise I'll have to write for more than 10-20 entities.

Upvotes: 1

Views: 2648

Answers (1)

Alessandro Desantis
Alessandro Desantis

Reputation: 14343

If all the methods in your entities will be getters or setters, you can use ReflectionObject to retrieve a list and access them dynamically:

$object = new \ReflectionObject($entityObject);

foreach ($object->getMethods() as $method) {
    // $method is a \ReflectionMethod instance
    // invoke it or save its name

    // ...
}

Upvotes: 7

Related Questions