Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Evert Pot:
Accessing protected properties from objects that share the same ancestry.
Sep 16, 2014 @ 16:19:23

In his latest post Evert Pot shows an interesting side effect of working with two objects from the same class: accessing protected properties from one instance to the other.

I realized something odd about accessing protected properties the other day. It's possible in PHP to access protected properties from other objects, as long as they are from the same class. [...] I always thought that protected strictly allows objects to access things from the current inheritence tree, but didn't realize that this also extends to other instances of the same object.

He includes a bit of sample code showing two object instances each being able to access the protected "val" property from the other. He also shows an example of how it works in two different objects, both that derive from a common ancestor. He shares a few other code examples showing this relationship and points out a few places where it could come in handy.

tagged: protected property object ancestor access

Link: http://evertpot.com/properted-properties-from-shared-ancestry/


Trending Topics: