0

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?

Rohit
  • 3,018
  • 2
  • 29
  • 58
  • What do you need access for? It sounds like you need to provide an access method and simply check the privileges of the current user before you return the value. – jeroen Nov 22 '17 at 14:41
  • I apologize for being unclear. A second class needs information from the first, but the user never should. The classes are siblings (from the same parent), so I can't use inheritance as far as I know. Based on your answer, I'm feeling like this has more to do with my lack of experience with reflection, but every tutorial/guide I find feels the same.I'm no closer to understanding practical use. – Rohit Nov 22 '17 at 14:44
  • Possible duplicate of [What is reflection and why is it useful?](https://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) – thehennyy Nov 22 '17 at 14:48
  • Thanks thehennyy. I actually did see that question, but as it seems to talk about the benefits in a strictly typed language, thought I'd ask about it in a PHP context. – Rohit Nov 22 '17 at 14:55
  • Using reflection to get the value of a property sounds wrong to me, I cannot think of an example where I would use that in a production environment. – jeroen Nov 22 '17 at 15:07

0 Answers0