PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Wojciech Sznapka's Blog: Accessing private object property from other object in PHP

Note: This article was originally published at PHPDeveloper on 17 November 2010.
PHPDeveloper

Wojciech Sznapka has a new post to his blog showing an interesting OOP programming strick related to accessing private properties in one object from another if they're derived from the same class.

Last time I wrote about Weirdest PHP construction I've ever seen, now I found another unusual PHP solution. PHP offers 3 visibility modifiers: private, protected and public. Private properties and methods can't be accessed outside the object, as well as from inherited classes. With one exception... They can be accessed by other instances of the same class. You can read more about it in the PHP manual.

In his code example he shows how to create two objects of the Foo class and, using getters and setters change the value of a private property from the first object on the second object.