Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP allow calling private methods from other instances?

PHP would let calls to private methods as long as they come from instances of the same class.

This is how the PHP docs explains it:

Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.

Doesn't this break encapsulation? How does the fact that implementation specific details are known justify it?

p.s. Are there other (object oriented) languages that do this?

like image 396
Emanuil Rusev Avatar asked Oct 22 '22 10:10

Emanuil Rusev


1 Answers

This is true for most languages, in java you can do it too, this is because the object is from the same instance, it "knows" all the properties same instances have

like image 163
Dima Avatar answered Nov 27 '22 22:11

Dima