In PHP, the class inheritance relationship is in PHP. I often write a class, which writes a shared method, and then allows the subclass to inherit the class to get the corresponding function. Suppose there is a parent class:
1
Then, let's create a subclass to inherit from him:
1
In this case, if you instantiate the subclass, the result will be the constructor of the subclass ~!
Of course, because of the inheritance relationship, sub-classes can call the say () method of the parent class.
If you want to implement the constructor of the parent class, but you want to implement the constructor of the child class, you can do this:
1
At this time, the constructor of my parent class will be output! And I am the constructor of the subclass ~.
Assume that you define such a method in the subclass:
1 protected function say ($ str = '') {2 echo 'Say I am a subclass '; 3}
In this case, the sys () method inherited from the parent class will be overwritten, so the result after the call is: I am a subclass of say.
What if you define it like this?
1 public function say ($ str = '') {2 echo 'I am a subclass method ~ '; 3}
This statement is acceptable. PHP is different from other strong-type languages in that the PHP rewrite method allows you to "upgrade to the public" method, rather than "privatize ", for example, if you define "private" here, an error is certainly returned, and the opposite is true for languages like C ++.
The idea of PHP for this situation is that your father gave you a protected level of heritage. at this time, you have a protected level. your father allows you to share with others, that is, public, but you cannot hide it yourself, that is, private.
At this point, you will certainly ask, what will happen if the say () of the parent class is set to private?
The final result is that the parent class has been privatized and the subclass cannot be inherited at all. Therefore, you can set the "say ()" method in the subclass as needed.
Another problem is that the parent class has defined sys () with optional parameters. what if you do not have a parameter definition like below in the subclass?
Public function say () {echo 'haha. I have no parameters ~ ';}
The results can still run normally, but there will be a prompt at the E_STRICT level. The reason is that the PHP standard means that the number of parameters must be aligned with the parent class. of course, you can set the error level in php. ini.
If there are any errors above, please correct them in time. thank you.