简体   繁体   English

为什么PHP允许从其他实例调用私有方法?

[英]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. 只要它们来自同一个类的实例,PHP就会调用私有方法。

This is how the PHP docs explains it: 这就是PHP文档解释它的方式:

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? implementation specific details are known的事实如何证明它implementation specific details are known正确的?

ps Are there other (object oriented) languages that do this? ps还有其他(面向对象的)语言吗?

对于大多数语言都是如此,在java你也可以这样做,这是因为对象来自同一个实例,它“知道”相同实例具有的所有属性

As an answer to the post scriptum , Delphi (so-called "object pascal") and Lazarus ("free pascal") both allow access to the private properties only in the same unit that the class implementation is coded. 作为post scriptum的答案,Delphi(所谓的“object pascal”)和Lazarus(“free pascal”)都允许仅在与类实现编码相同的单元中访问私有属性。 So you can insert two different classes inside one unit and they have access to each other's private properties. 因此,您可以在一个单元中插入两个不同的类,并且可以访问彼此的私有属性。

It is not allowed if two classes are placed in different units (let's say it's something like a namespace in PHP). 如果两个类放在不同的单元中是不允许的(让我们说它类似于PHP中的命名空间)。

As an answer to "Doesn't this break encapsulation?": 作为“这不打破封装吗?”的答案:

Although the Encapsulation principle in OOP definition is a bit vague. 虽然OOP定义中的封装原则有点模糊。 The way I understand it is it keeps private data and logic in the bounds of a class. 我理解它的方式是它将私有数据和逻辑保存在类的边界内。

Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties direct access to them. 封装用于隐藏类中结构化数据对象的值或状态,防止未授权方直接访问它们。

I don't know if this behaviour of classes in PHP, Java and other languages is good or bad, but I don't think it breaks the Encapsulation of the classes . 我不知道PHP,Java和其他语言中的这种类的行为是好还是坏,但我不认为它破坏了类封装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP是否允许在其他函数/方法中进行getter调用? - Does PHP allow getter calling in other functions/methods? Php/OOP 如何避免同一个类的实例访问其他对象的私有属性/方法 - Php/OOP How to avoid instances of the same class to access private properties/methods from other objects 为什么PHP允许静态调用非静态方法,却不允许静态调用非静态属性? - Why does PHP allow calling non-static methods statically but does not allow calling non-static properties statically? 为什么PHP允许通过子类中的覆盖使受保护的和私有的方法公开? - Why does PHP allow protected and private methods to be made public via an override in subclasses? 为什么PHP允许你在构造函数上重载类型提示而不允许在方法上重载? - Why does PHP allow you to overload typehints on constructors but not on methods? php-为什么没有实例化就无法从同级类调用静态方法? - php - Why calling static methods from sibling classes does not work without instantiation? 为什么ReflectionClass :: getMethods()从父类返回私有方法? - Why does ReflectionClass::getMethods() returns private methods from parent class? 为什么PHP允许覆盖私有方法? - why php allow to overwriting private method? php对象交互-在其他实例中调用实例 - php objects interaction - calling instances within other instances 从Php调用Java方法 - Calling Java Methods from Php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM