Something that comes up every so often is how to convert the name of a property into a getter or setter, and generally the solution proposed is something like the following.
1 2 3 4 |
|
While this might be an acceptable method, there are more elegant solutions, one of which is using the PropertyPath utility provided by the Symfony2 Form component.
The PropertyPath utility provides more advanced functionality, which will search for a getter, isser or hasser method, then fall back to trying to access the property itself.
Consider the following object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Should you need to access information on this object without knowing in advance which properties you’d like to call, PropertyPath can help you out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Chaining calls
Another feature of the PropertyPath utility is that it will let you
chain calls down an object graph. Consider you wanted to make a call
that would be converted to $bar->getPatrons()->getFirst()->getName()
,
you could use the property path patrons.first.name
when constructing
the PropertyPath.