mercredi 22 novembre 2017

Reflection vs public method

While I've learned/studied reflection in the past, I never really dove into use cases (and I'm honestly not sure I'm understanding it's value now either). However, while working on a problem for a personal project, I asked a coworker for thoughts on a problem I was stuck on and he suggested reflection. After some review and doc reading, it got me thinking:

The core of my problem is a property that is protected static, and thus can't be accessed outside the class. However, it's turning out I need access to it and while a user of the package would never need to use a method I setup to get the property, I also don't see why I should give them access to the data directly as such. It makes it seem that reflection is a good pattern to follow here, to allow access to the property through the Reflection class, but at the same time, it makes me wonder about just making a public getter for the field.

Am I looking at reflection wrong? From a number of examples I've seen online, I wonder why use reflection instead of making getters? Is it a PHP thing, where other languages (specially typed languages) can use reflection more usefully?





Aucun commentaire:

Enregistrer un commentaire