PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I recall reading an article a while back on PHP5 having a loophole (using one of the class functions?) which allowed manipulation of private/protected variables as though they were normally accessible member variables...
Anyone know what function I might look at?
I've been tinkering with PHP 5.1.6?? and only on some variable names do I get an access error when accessing a member variable which is private - other times it appears as though the member is being added as an expando property (which I realize is expected behviour - but shouldn't I get an error when trying that on existing properties?) in which case I have duplicated variables one marked as private the other as public???
class test{
}
$obj = new Class1();
$obj->_obj = new test();
// Results in the following print_r() dump:
Class1 Object
(
[_file] =>
[_isCachable] =>
[_obj] =>
[_obj] => test Object
(
)
)
Edit: Maybe I am remembering wrong, perhaps it was only for getting and *not* setting a member variable...hehe...frig...ah well...back to my original design I guess
Might work, but...it also sounds as though that loop hole has been patched?
It's not for unit testing but a framework...I really just need to set a private value once immediately after the object is created by the framework...private so it stay hidden in all derived classes.
I'm sure it was something different than the above...if I remember correctly it was one of the object enumeration type functions maybe which returns an object...and using that allowed you to update the property?
I can't remember....maybe it wasn't even PHP...hmmmmmm...
feyd wrote:Why not provide a mutator with some access restriction controls?
Thats what I ended up doing, using a local static counter to prevent setting more than once...
The main reason for me wanting to use the 'hack' mentioned above was because I wanted to hide that detail from anyone ever deriving from the base class. The method (despite being final) was still call-able in derived classes it just wouldn't do anything.
The detail is really only set by the framework and so hiding any knowledge of it's existence in derived classes was my goal.
I ended up re-implementing and coming up with a better solution - so all is good