开发者

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开发者_Go百科();   // 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.


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

    // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜