Symfony2 - 如何动态访问实体方法?

Edg*_*dge 1 php symfony doctrine-orm

我想通过调用它的对象来动态访问Symfony2实体方法.例如:

$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;
}
Run Code Online (Sandbox Code Playgroud)

如果整理出来!我确信这个程序会帮助我编写更少的代码,否则我将不得不为超过10-20个实体编写代码.

Ale*_*tis 7

如果实体中的所有方法都是getter或setter,则可以使用ReflectionObject检索列表并动态访问它们:

$object = new \ReflectionObject($entityObject);

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

    // ...
}
Run Code Online (Sandbox Code Playgroud)

  • 这样:``$ method-> invoke($ entityObject,'argument');``. (3认同)