Modifying protected properties in PHP

6 July 2011

In a blog post, Matthew Weier O’Phinney pointed out that if class Derived extends class Base, Derived can access pro­tected prop­er­ties and methods of Base even in the case where an in­stance of Base is merely the ar­gu­ment to some method. (I ex­pected Derived to be able to access $this->foo where $foo is protected, but I didn’t expect that Derived would be able to access $obj->foo where $obj is an argument.)

This works even if the func­tions are static, leading to a generic way to modify pro­tected properties: