Should an object method have access to a protected property of another object of the same class?
I am coding in PHP, and I just found that access to an object-protected property is allowed using a method of the same class, even if it is not of the same object.
In this example, you first get β3β in the output - because the readOtherUser function will successfully access the value - and after that a fatal PHP error will occur - because the main program will have failed access with the same value.
<?php class user { protected $property = 3; public function readOtherUser () { $otherUser = new user (); print $otherUser->property; } } $user = new user (); $user->readOtherUser (); print $user->property; ?>
Is this a PHP bug or is it supposed behavior (and I will have to retrain this concept ... :)) (and are there any links to this fact)? How is this done in other programming languages?
Thanks!
visibility object php class instance
JΔnis elmeris
source share