A Simple Example of PHP Object Oriented Richter Substitution Principle

  • 2021-09-20 19:44:31
  • OfStack

This paper illustrates the Richter substitution principle of PHP object-oriented. Share it for your reference, as follows:

Richter Replacement Principle (Liskov Substitution Principle)

Richter's substitution principle tells us that if a base class object is replaced by its subclass object in software, the program will not produce any errors and exceptions, and the reverse is not true. If a software entity uses a subclass object, it will not be able to use the base class object. Richter substitution principle is one of the important ways to realize the open-close principle. Because subclass objects can be used wherever base class objects are used, the object is defined by using base class types as much as possible in the program, and its subclass types are determined at runtime, and the parent class objects are replaced by subclass objects.

When using the Richter substitution principle, note that all methods of a subclass must be declared in the parent class, or the subclass must implement all methods declared in the parent class. Try to design the parent class as an abstract class or interface, Let the subclass inherit the parent class or implement the parent interface, and implement the method declared in the parent class. When running, the subclass instance replaces the parent class instance. We can easily extend the function of the system without modifying the code of the original subclass. Adding new functions can be realized by adding a new subclass.

Understand through 1 piece of code


<?php
// Example 1
class Bird{
  protect function fly(){
  }
}
// Kingfisher 
class KingFisher extends Bird{
}
// Ostrich 
class Ostrich extends Bird{
  // Ostriches can't fly 
}
// Example 2
class A{
  protect function add($a, $b){
    return $a + $b;
  }
} 
// Heavy load 
class B extends A{
  protected function add($a, $b){
    return $a + $b + 100;
  }
}
?>

Richter substitution principle is a constraint on class inheritance. There are two understandings of the Richter substitution principle:

1. You can't just inherit inappropriate classes with redundant methods or attributes. (Example 1)

2. Subclasses can extend the functions of the parent class, but cannot change the original functions of the parent class. (Example 2)

The Richter substitution principle contains the following hidden meanings:

A subclass can implement the abstract method of the parent class, but it cannot override the non-abstract method of the parent class.

② Subclasses can add their own unique methods.

When the method of the subclass overloads the method of the parent class, the precondition of the method (i.e. the formal parameters of the method) is looser than the input parameters of the parent class method.

4. When the method of the subclass implements the abstract method of the parent class, the post condition of the method (i.e. the return value of the method) is stricter than that of the parent class.

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 Syntax", "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: