PHP Richter Replacement Case detailed

Source: Internet
Author: User
This time to everyone to bring PHP on the Richter replacement case, the PHP Richter replacement note what, here is the actual case, take a look.

Richter Replacement principle (Liskov Substitution Principle)

The Richter substitution principle tells us that in the software a base class object is replaced with its subclass object, the program will not produce any errors and exceptions, and conversely, if a software entity uses a subclass object, then it is not necessarily able to use the base class object. The Richter replacement principle is one of the important ways to realize the open and closed principle, because the subclass object can be used wherever the base class object is used, so as far as possible the base class type is used in the program to define the object, and at run time the subclass type is determined, and the subclass object is substituted for the parent class object.

When using the Richter substitution principle, it is important to note that all methods of a subclass must be declared in the parent class, or the subclass must implement all the methods declared in the parent class. As far as possible 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, the runtime, 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, add new function can be implemented by adding a new subclass

Learn from a piece of code

<?php//Example 1class bird{  protect function Fly () {  }}//Kingfisher class KingFisher extends bird{}//Ostrich class ostrich  Extends bird{  //Ostrich can't fly.}//example 2class a{  protect function Add ($a, $b) {    return $a + $b;  }}//Reload Class B extends a{  protected function Add ($a, $b) {    return $a + $b +;  }}? >

The Richter substitution principle is a constraint on class inheritance. There are two ways to understand the Richter substitution principle:

1. Do not arbitrarily inherit the inappropriate, redundant methods or attributes of the class. (Example 1)

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

The Richter replacement principle contains a few hidden meanings:

The ① subclass can implement an abstract method of the parent class, but cannot overwrite the non-abstract method of the parent class.

You can add your own unique methods to ② subclasses.

③ when a method of a subclass overloads a method of the parent class, the method's preconditions (that is, the parameter of the method) are more lenient than the input parameters of the parent class method.

④ when a method of a subclass implements an abstract method of the parent class, the post condition of the method (that is, the return value of the method) is stricter than the parent class.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Cache Tool XCache Installation and use case resolution

How PHP prevents concurrent repeat writes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.