Home  >  Article  >  Backend Development  >  PHP object-oriented - sample code sharing of subclass extending parent class (subclass reloading parent class)

PHP object-oriented - sample code sharing of subclass extending parent class (subclass reloading parent class)

黄舟
黄舟Original
2017-03-25 10:00:441605browse

In PHP, you will encounter such a situation. The subclass inherits the parent class, but you need to make certain attributes and methods of the parent class. extension, then the subclass can rewrite the properties and methods, covering the properties and methods with the same name as the parent class. However, if the content of the parent class's method is relatively large, such as hundreds or thousands of lines of code, then only Use "parent class name::method" or "parent::method" to call the overridden method in the parent class , the only thing is to reload the parent class, and then add the statements that need to be expanded.

Overriding of methods

<?php
 class Person{
       public $name;       
       public function construct($name="" ){         
               $this->name=$name;                                             
        }        public  function say(){
             echo "我叫".$this->name  ;  
        } 

}?><?php
     class Student extends Person{
          public $name;               

          public function construct($name=""){
               $this->name =$name;
          }//这里定义了一个和父类中同名的方法,将父类中的说话方法覆盖并重写
    public  function say(){
             echo "我叫".$this->name .",今年25岁了" ;  
    } 
}?>

Overriding methods and access rights

When a subclass overrides a method of the parent class, please note that the access rights of the method overridden in the child class must not be lower than the access rights of the overridden method of the parent class. For example, the access permission of a method in the parent class is protected, then the permission of the method overridden in the subclass must be protected or public. If the method of the parent class has public permission, the method to be overridden in the subclass can only be public. In short, when overriding a method of the parent class in a subclass, it must be higher than the permissions of the overridden method of the parent class.

Number of parameters when overriding

Subclasses can have a different number of parameters than the parent class, such as the following Constructor method In , an additional parameter $age is added.

<?phpclass Student extends Person{

    public $name;    
    public $age;        

    public function construct($name="",$age=25){

         $this->name =$name;         $this->age =$age;

    }    public  function say(){

         echo "我叫".$this->name .",今年".$this->age."岁了" ;  

    } 

}?>

In the above example, we have achieved the extension of "method" by overriding it.
However, although doing this solves the problem we mentioned above, in actual development, a method cannot be just one code or several codes, such as the "say()" method in the "Person" class There are 100 lines of code in it. If we want to overwrite this method and retain the original functions and add a little more functionality, we have to rewrite the original 100 lines of code, plus a few expanded lines of code. This is still Okay, but in some cases, the method in the parent class cannot see the original code. How do you rewrite the original code at this time? We also have a solution, that is, in the subclass method, you can call the overridden method in the parent class. That is, you can take the original functions of the overridden method and add some of your own functions. There are two methods. Implement the method of calling the overridden method of the parent class in the method of the subclass:

One is to use the "class name::" of the parent class to call the overridden method of the parent class;
One is Use the "parent::" method to call the overridden method in the parent class;
 

Extension of the method

<?phpclass Student extends Person{

    public $name;    
    public $age;                                 

    public function construct($name="",$age=25){

        parent::construct($name,$age);        $this->age =$age;

    }    public  function say(){

        parent::say();        echo ",今年".$this->age."岁了" ;  

    } 

}?>

What is described above is just reloading the attributes and methods of the parent class. It is not overloading in the true sense. It can only be said that the subclass is reloading the parent class. Expanded, in PHP, the word overloading also exists, but the meaning of overloading is different from that in general language-oriented languages.

The above is the detailed content of PHP object-oriented - sample code sharing of subclass extending parent class (subclass reloading parent class). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn