PHP Object-Oriented Programming Subclass Extended Parent Class (Subclass Reloaded Parent Class) Operation Detailed

Time:2019-8-11

This paper illustrates the operation of extending parent class (subclass reloading parent class) in PHP object-oriented programming. Share for your reference, as follows:

In PHP, we will encounter the situation that the subclass inherits the parent class, but it needs to expand the attributes and methods of the parent class. At this time, the subclass can override the attributes and methods of the same name as the parent class, but if there are more contents in the method of the parent class, such as hundreds of thousands of lines of code. At this point, you just need to use “Parent Class Name: Method” Or“Parent:: Method“To call the overridden method in the parent class, there areReloading parent classThen add the statement that needs to be extended.

Method rewriting

<?php
 class Person{
  public $name;
  public function __construct($name="" ){
    $this->name=$name;
  }
  public function say(){
    Echo "My name". $this - > name;
  }
}
?>
<?php
  class Student extends Person{
   public $name;
   public function __construct($name=""){
    $this->name =$name;
   }
// This defines a method with the same name as that in the parent class, which overrides and overrides the speech method in the parent class.
 public function say(){
    Echo: "My name is". $this - > name.""25 years old this year";
 }
}
?>

Rewrite Method and Access Permission

When the subclass overrides the method of the parent class, it should be noted that the access rights of the method overridden in the subclass must not be lower than that of the method overridden by the parent class. For example, the access rights of methods in the parent class areprotectedThe permissions of methods overridden in subclasses areprotectedorpublic。 If the parent class’s method ispublicThe only way to override permissions in subclasses ispublic。 In a word, when overriding the method of the parent class in the subclass, it must be higher than the permission of the method covered by the parent class.

Number of parameters when rewriting

Subclasses can have a different number of parameters than their parent classes. In the following construction method, an additional parameter, $age, is added.

<?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: $this - > name. "$this - > age."$this - > age. "
 }
}
?>

In the example above, we implemented the “method” extension by covering it.
However, although doing so solves the problem we mentioned above, in actual development, a method can not have a single code or several codes, such as the “say ()” method in the “Person” class, which has 100 codes, if we want to cover the method with the original function and add a little bit more. Function, we need to rewrite the original 100 code once, plus a few extended code, which is good, and in some cases, the method in the parent class can not see the original code, how do you rewrite the original code at this time? We also have a solution, that is, in the method of subclass, we can call the method overridden in the parent class. That is, we can call the method overridden in the method of subclass by two methods, which is to take the original function of the overridden method and add one of our own functions.

One is to use the parent class.“Class name:” To call the overridden method in the parent class;

One is to use“parent::” The square test calls the overridden method in the parent class.

Extension of methods

<?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. "old";
 }
}
?>

All that is described above isReloadThe attributes and methods of the parent class are not overloading in the real sense. It can only be said that the subclass extends the parent class. In php, there is also the word overloading, but it is different from the overloading in general language-oriented.

PHP overloading reference: PHP object-oriented overloading

Simulating method overload in general object-oriented languages in php, please refer to: PHP object-oriented _Simulating method overload in general object-oriented languages

More readers interested in PHP-related content can see the topics of this site: Introduction to Php Object-Oriented Programming, Introduction to PHP Array Operating Skills, Introduction to PHP Basic Grammar, Summary of PHP Operating and Operator Usage, Summary of Php String Usage, and php+mysql Data. Introduction to Library Operations and Summary of Common Database Operating Skills in PHP

I hope this article will be helpful to everyone’s PHP program design.

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]