Should an object's method be able to access a protected property of another object of the same class?
I'm coding in PHP, and I just discovered that an object's protected property is allowed to be accessed by a method of the same class even if not of the same object.
In the example, at first, you'll get "3" in the output - as function readOtherUser will have successfully accessed the value -, and after that a PHP fatal error will occur - as the main program will have failed accessing 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 the intended behaviour (and I'll have to relearn this concept… :)) (and are there references to the fact)? How is it done in other programming languages?
Thanks!