PHP study notes 5-class inheritance/method rewriting, study notes 5-
Change the Man. php file name to People. php and add the following code:
1 public function hi () {2 return $ this-> _ name. 'Say Hi'; 3}
Create a file Man. php:
1 <? Php 2/*** Created by PhpStorm. 4 * User: Administrator 5 * Date: 6 * Time: 7 */8 9 require_once 'people. php '; 10 11 class Man extends People {// extends: inherits the People class 12 public function _ construct ($ age, $ name) {13 parent :: __construct ($ age, $ name, 'male'); 14} 15 16 public function hi () {// rewrite the hi method of the People Class 17 // echo parent :: hi (); // preserve the parent class hi Method 18 echo 'man '. $ this-> getName (). 'Say Hi'; 19} 20}
If you keep the parent class method, you can write it like this.Parent: hi ();This field is not retained. Add the following line.
Modify the index. php file:
1 require_once 'man. php'; 2 $ m = new Man (10, 'vito '); 3 echo $ m-> hi ();
Finally, we can see the success output on the webpage: Man Vito say hi.