简体   繁体   English

为什么PHP允许通过子类中的覆盖使受保护的和私有的方法公开?

[英]Why does PHP allow protected and private methods to be made public via an override in subclasses?

From some brief fiddling about, I find I get an error when overriding superclass methods in a subclass when I do the following: 从一些简短的提示,我发现当我执行以下操作时覆盖子类中的超类方法时出现错误:

  • Override a protected superclass method with a private subclass method 使用私有子类方法覆盖受保护的超类方法
  • Override a public superclass method with a protected or private subclass method 使用protected或private子类方法重写公共超类方法

However, If I do it in the other direction, no error is thrown: 但是,如果我在另一个方向执行此操作,则不会抛出任何错误:

  • Overriding a private superclass method with a protected or public subclass method 使用受保护或公共子类方法覆盖私有超类方法
  • Overriding a protected superclass method with a public subclass method 使用公共子类方法覆盖受保护的超类方法

This seems counter intuitive to me - I was expecting it to work the other way around in order to enforce information hiding and encapsulation. 这对我来说似乎很直观 - 我希望它能以相反的方式工作,以强制执行信息隐藏和封装。 This appears to allow poor design by allowing internals to be exposed in a way that may break other methods and I can't see a case when this would be a good idea. 这似乎允许糟糕的设计,允许内部以可能破坏其他方法的方式暴露,我不能看到这是一个好主意的情况。 Why has it been implemented this way and what have I missed? 为什么以这种方式实施,我错过了什么?

Also, is this standard practice in other programming languages? 此外,这是其他编程语言的标准做法吗?

What you call "enforce information hiding" is something, that may break subclasses, because suddenly properties and methods may disappear. 你所谓的“强制信息隐藏”是可能会破坏子类的东西,因为突然的属性和方法可能会消失。 You cannot break things by loosing restrictions this way. 你不能通过这种方式放松限制来破坏事物。

With private is a bit different: In this case the property/method does not exists from the child class' point of view. private有点不同:在这种情况下,从子类的角度来看,属性/方法不存在。 Thus there is no reason, why a subclass may not introduce a property with that name, because it will be a different property. 因此没有理由,为什么子类可能不会引入具有该名称的属性,因为它将是一个不同的属性。

You are right, that this may lead to poor design, but you can always build an application with poor design. 你是对的,这可能会导致糟糕的设计,但你总是可以构建一个设计不佳的应用程序。

You can't decrease the visibility of class members, only increase them. 您不能降低类成员的可见性,只能增加它们。

Imagine a class A which has a public method, and a subclass B would make it private. 想象一下,一个A类有一个公共方法,一个子类B会把它变成私有的。 B could be treated as a type A, for which it is assumed that it has that public method. B可以被视为类型A,假设它具有该公共方法。

If you think expossing a private/protected method in a subclass represents a problem, just make the method final. 如果您认为在子类中公开private / protected方法表示存在问题,那么只需将方法设为final即可。

See it this way: by NOT making a method final, you are allowing any subclass to overriding it at will; 这样看:通过使方法最终,你允许任何子类随意覆盖它; this obviously includes increasing its visibility. 这显然包括提高其知名度。

If a superclass exposes a method publicly, then all its subclasses must also expose the same method (or an overridden version), therefore reducing the accessibility of a method is a subclass is illegal in PHP (and pretty much every class-based OO language). 如果一个超类公开公开一个方法,那么它的所有子类也必须公开相同的方法(或一个被覆盖的版本),因此减少方法的可访问性是一个子类在PHP中是非法的(几乎每个基于类的OO语言) 。

The opposite is not true, so increasing the accessibility of methods in subclasses is perfectly fine. 相反的情况并非如此,因此增加子类中方法的可访问性是完全正确的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM