Dealing with private methods
One of the reasons why developers create a super class is to exert a certain degree of control over the method signatures of subclasses. During the parsing phase, PHP normally confirms that the method signatures match. This leads to proper use of your code by other developers.
In the same vein, it does not make sense to have PHP perform the same rigorous method signature check if a method is marked as private
. The purpose of a private method is that it is invisible to the extending class. If you define a method of the same name in the extending class, you should be free to define it at will.
To illustrate this problem, let's define a class called Cipher
with a private method called encrypt()
. The OpenCipher
subclass redefines this method, causing a fatal error when running under PHP 7:
- First, let's define a
Cipher
class whose constructor generates random values for$key
and$salt
. It also defines a public method calledencode()
that...