1

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.

2
  • Will your entities also contain methods that are not getters/setters? For example, are you implementing some interface like Symfony\Component\Security\Core\User\UserInterface? Commented Oct 10, 2011 at 14:56
  • those are getters/setters for sure and those I want to access dynamically, by keeping in mind that I don't know their names! Commented Oct 10, 2011 at 15:09

1 Answer 1

7

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

    // ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

by the way, I can see the method names, but how can I use both get/set methods to access/insert data?
This way: $method->invoke($entityObject, 'argument');.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.