Why can var_dump ascertain values of private variables, yet it can't when trying to access a single the property

前端 未结 3 921
生来不讨喜
生来不讨喜 2021-01-06 16:37

I have an object that is being thrown into the session array, and I want to run a foreach on the items property.

I can\'t seem to access it. I see that it\'s priva

3条回答
  •  暖寄归人
    2021-01-06 17:03

    That's the idea of private properties: You can't access them. You should really not break this concept. If you really want to access such property, mark is as "public" in the original class definition.

    The reason why var_dump can access it is because it is an internal function, and it has the "power" to view the whole object. However, your code doesn't have that power.

    I wouldn't recommend it, but if you really need to access a private property, you can use PHP Reflection to achieve it.

提交回复
热议问题