Forum Moderators: coopster

Message Too Old, No Replies

Get visibility of a class variable from within the class

         

Readie

4:05 pm on Sep 10, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm just wondering if this is even possible. None of my colleagues knew, and I couldn't find anything from a (admittedly kinda quick) Google search.

Basically, I'm wondering if there is anyway to detect if a variable is static or not, and if a variable is public, protected or private, from within their class.

The reason I'd like to know is so I can be safe with overwriting the __set() method with a call like $this->$key = $val as the default: in a switch statement, and not give external scripts the ability to alter private and protected variables.

penders

1:40 am on Sep 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The reason I'd like to know is so I can be safe with overwriting the __set() method with a call like $this->$key = $val as the default: in a switch statement, and not give external scripts the ability to alter private and protected variables.


Bit of a flaw with this... the __set() method is only called for inaccessible properties anyway. It will never get called for public properties.

You can get the visibility (and other info) of class properties using The ReflectionProperty class [uk3.php.net], however, I don't think you want to do this.

Readie

6:12 pm on Sep 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It will never get called for public properties.

Ahh. Right. Makes all of this a bit pointless then :)

Thanks for the pointer with the reflection property class though, I'll have a play. If not for any legitimate use in this instance, may come in use later :)