![]() |
Accessing data in protected and private properties
Today I decided to look though a small trick by Derick Rethans on how to access private properties in PHP and I ended up making a function to collect Reflection-like information and wanted to share the source:
PHP Code:
PHP Code:
Its also however possible to change the values, BUT when you cast them back to an object the class will be changed to an instance of the 'stdClass' class and therefore it will loose all its methods and isn't useful any longer, so this trick is mainly for debugging proposes ;) Hope this will help some! |
As expected from Kalle, some very nice piece of PHP. :)
|
On a side note. If you have wrote your program in such a way that you ever need to bring private class variables outside of the class, you need to rethink your design. Private variables are made for the specific purpose of not being seen by anything, so to need to see them is a sign of bad architecture.
|
The code was written for debugging proposes where it sometimes is needed to check the values of private/protected properties to make sure everything is in place.
|
| All times are GMT. The time now is 12:30 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0