PHP Object Oriented Programming Subclass Extension Parent Class (Subclass Reload Parent Class) Operation Detailed Explanation

  • 2021-12-12 03:47:15
  • OfStack

This article illustrates the PHP Object-Oriented Programming Subclass Extension Parent (Subclass Reload Parent) operation. Share it for your reference, as follows:

In PHP, Will encounter such a situation, The child class inherits the parent class, However, it is necessary to extend the attributes and methods of the parent class, At this time, subclasses can override properties and methods, Override properties and methods with the same name as the parent class, but if there are many contents in the methods of the parent class, such as hundreds of thousands of lines of code, you only need to use "parent class name:: method" or "parent:: method" to call the overridden methods in the parent class, or reload the parent class, and then add statements that need to be extended.

Method overrides


<?php
 class Person{
  public $name;
  public function __construct($name="" ){
    $this->name=$name;
  }
  public function say(){
    echo " My name is ".$this->name ;
  }
}
?>


<?php
  class Student extends Person{
   public $name;
   public function __construct($name=""){
    $this->name =$name;
   }
// This defines the 1 Method with the same name as the parent class, overrides the speaking method in the parent class and overrides 
 public function say(){
    echo " My name is ".$this->name ." , this year 25 Twenty five years old " ;
 }
}
?>

Override Methods and Access Permissions

When a subclass overrides a method of the parent class, note that the method overridden in the subclass must not have less access than the overridden method of the parent class. For example, if the method in the parent class has access to protected, the method overridden in the subclass has access to protected or public. If the method of the parent class is public permission, the method to be overridden in the subclass can only be public. In summary, when overriding a parent's method in a subclass, 1 must be higher than the permissions of the parent's overridden method.

Number of parameters when overridden

A child class can have a different number of parameters than the parent class, such as adding an extra parameter $age in the following constructor.


<?php
class Student extends Person{
 public $name;
 public $age;
 public function __construct($name="",$age=25){
   $this->name =$name;
   $this->age =$age;
 }
 public function say(){
   echo " My name is ".$this->name ." , this year ".$this->age." Twenty five years old " ;
 }
}
?>

In the above example, we extended the "method" by overriding.
However, although doing so solves the problems we mentioned above, But in actual development, A method can't be just one code or several codes. For example, the "say ()" method in the "Person" class has 100 pieces of code in it. If we want to overwrite this method and keep the original function and add a little function, we must rewrite the original 100 codes once, and add a few extended codes, which is still good. In some cases, the methods in the parent class can't see the original code. How can you rewrite the original code at this time? We also have a solution, that is, we can call the overridden method in the parent class in the subclass method, that is, we can take the original function of the overridden method and add our own 1-point function, and we can call the overridden method of the parent class in the subclass method through two methods:

1 is to use the parent class " 类名:: "To call the overridden method in the parent class;

One is to use " parent:: "To call the overridden method in the parent class;

Extension of method


<?php
class 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 year ".$this->age." Twenty five years old " ;
 }
}
?>

What is described above is only to reload the attributes and methods of the parent class, not to overload in the true sense. It can only be said that the subclass extends the parent class. In php, there is also the word overload (overloading), but its meaning is different from that of overload in a general language.

Overload in php (overloading) Reference: PHP Object Oriented Overload (overloading)

Method overloading in php simulation 1 general object-oriented language, please refer to: PHP object-oriented _ simulation 1 general object-oriented language method overloading

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: