In PHP, I often write a class that writes a shared method, and then let the subclasses inherit it. Corresponding functions can be obtained. Suppose there is roughly such a parent class:
1 <?<span>php 2 class<span> Father{ 3 4 public function<span> __construct(){ 5 echo '我是父类的构造方法!'<span>; 6 <span> } 7 8 protected function say($str = 'Hello World!'<span>){ 9 echo '说了一句话:' . $str<span>; 10 <span> } 11 <span>} 12 ?></span></span></span></span></span></span></span></span></span>
Then, make a subclass to inherit it:
1 <?<span>php 2 3 class chirld extends<span> Father{ 4 5 public<span> __construct(){ 6 echo '我是子类的构造方法~'<span>; 7 <span> } 8 }</span></span></span></span></span>
At this point you instantiate the subclass, the result will be 我是子类的构造方法~
!
Of course, because of the 继承
relationship, the subclass can call the say()
method of the parent class.
If you want to implement the constructor of the parent class, but you also want to implement the constructor of the subclass, you can do it like this:
1 <?<span>php 2 3 class chirld extends<span> Father{ 4 5 public<span> __construct(){ 6 parent::<span>__construct(); 7 echo '我是子类的构造方法~'<span>; 8 <span> } 9 <span>} 10 11 ?></span></span></span></span></span></span></span>
At this time, 我是父类的构造方法!
and 我是子类的构造方法~
will be output.
Suppose you define such a method in a subclass:
1 protected function say($str = ''<span>){ 2 echo '我是子类的say'<span>; 3 }</span></span>
Then, the sys()
method you inherited from the parent class will be overridden, so the output result after the call is: 我是子类的say
.
What would happen if you defined it this way?
1 public function say($str = ''<span>){ 2 echo '我是子类的方法哦~'<span>; 3 }</span></span>
This way of writing is still acceptable. The difference between PHP and other strongly typed languages is that the PHP overriding method allows you to "publicize upwards" the overridden method, but does not allow you to "privatize downwards". For example, here you define private
to be An error will definitely be reported, but in a language like C it is exactly the opposite.
PHP’s thinking about this situation is that your father gave you a protected-level inheritance. At this time, it is a protected-level inheritance in your hands. Your father allows you to share it with others, that is, public
. But you are not allowed to hide it yourself, that is private
, and you definitely can’t keep it all to yourself.
At this point you will definitely ask, what will happen if the parent class's say()
is set to private
?
The result will not be the same, that is, the parent class has been privatized and the subclass cannot inherit it at all, so you can customize the say()
method in the subclass as you like.
There is another problem, that is, the parent class has already defined sys()
with optional parameters. So, what will happen if you define it in the subclass without parameters like the following?
public function<span> say(){ echo '哈哈。我没有参数了耶~'<span>; }</span></span>
The result can still run normally, but there will be a E_STRICT
level prompt. The reason is that the PHP standard is that the number of parameters must be aligned with the parent class. Of course, the error level can be set in php.ini.
If there are any errors in the above, please correct them in time, thank you.