PHP object-oriented programming-in-depth understanding of Method overloading and method coverage (polymorphism), polymorphism overlay _ PHP Tutorial

Source: Internet
Author: User
PHP object-oriented programming-deep understanding of Method overloading and method coverage (polymorphism), and multi-state coverage. PHP object-oriented programming-in-depth understanding of Method overloading and method coverage (polymorphism), what is polymorphism? Polymorphism literally means multiple states. PHP object-oriented programming-in-depth understanding of Method overloading and method coverage (polymorphism), and multi-state coverage

What is polymorphism?

Polymorphism literally means "multiple states ". In Object-Oriented Language, multiple implementation methods of interfaces are polymorphism. Reference Charlie Calverts's description of polymorphism-polymorphism is a technology that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features assigned to its sub-objects (from "Delphi4 programming technology insider "). To put it simply, you can assign a pointer of the subclass type to the pointer of the parent class (that is, Baidu Encyclopedia ). So what is the role of polymorphism and what is its actual development value? In actual application development, the polymorphism in object orientation is mainly used to treat different subclass objects as a parent class, and the differences between different subclass objects can be shielded, write common code and make general programming to adapt to the changing needs.

Below are two implementations of polymorphism in PHP.

Method overload)

Overload is an implementation of class polymorphism. Function overload means that an identifier is used as multiple function names and can be used to distinguish these functions with the same name by the number or type of function parameters. the call is not obfuscated. That is, although the method name is the same during the call, the corresponding function can be automatically called according to different parameters.

class A{public function test(){echo "test1";}public function test($a){echo "test2";}}$a=new A();$a->test();$a->test($a);

If php directly supports method overloading. After the preceding example is executed, different values are returned if the parameter is passed or the parameter is not passed. However, php does not directly support overloading, which means that if you define it as above, an error will be reported. What error will be reported? The following error is reported.

This means that function A cannot be repeatedly defined, and the number of rows that report an error is exactly the following line.

public function test($a){

Therefore, php does not directly support overloading. Php does not support it for so long .. Don't worry. I am not talking about direct support, so we can make php support indirectly. At this time, we need to use a function to support overloading. It is _ call (). The _ call () method must have two parameters. The first contains the name of the called method, and the second parameter contains an array of parameters passed to the method. You can use this method to implement functions similar to function overloading. See the following code.

Public function _ call ($ method, $ p) {if ($ method = "display") {if (is_object ($ p [0]) {$ this-> displayObject ($ p [0]);} else if (is_array ($ p [0]) {$ this-> displayArray ($ p [0]);} else {$ this-> displayScalar ($ p [0]) ;}}
// The following is the call to the above definition $ ov = new overload; $ ov-> display (array (1, 2, 3); $ ov-> display ('cat ');

When defining a method, you can see that there are three branches. If an object is passed to the display () method, the displayObject () method is called. If an array is passed, call displayArray (); if other content is passed, the displayScalar () method is called... We can see that when the following call is performed, the first is to pass an array, and displayArray () is called (). If the second input is not an object or an array, it belongs to other content and the displayScalar () method is called. Therefore, the _ call () method is used to implement method overloading similar to other languages.

Override)

The so-called overwrite is essentially a rewrite. That is, when the subclass inherits some methods of the parent class, and the subclass defines the same method internally, the newly defined method will overwrite the inherited methods of the parent class, subclass can only call its internally defined methods.

There are the following requirements:

1. when a parent class and a subclass have a method with the same parameters and names, the subclass method will overwrite the method of the parent class.

2. when method override is implemented, the access modifier can be different, but the access range of the subclass must be greater than or equal to the access range of the parent class.

3. the parameters and names must be the same. The parent class name is the same.

The following are some explanations:

The first point must have the same parameters to implement method override. If the number of parameters is different, an error is returned (this involves the method overload mentioned above ). If the method names are inconsistent, the method will not be overwritten, but the new method defined by the Subclass .;

Second, this is the design rules for php. What I understand is that it is easier to access things on the higher layer. if you access things on the lower layer, the permissions must be higher.

Check the code:

Class people {protected function sing () {echo "" ;}} class woman extends people {public function sing () {echo "";}} $ woman1 = new woman (); $ woman1-> sing ();

In this way, "Singing for women" can be output normally ". However, when the sing () method in woman is changed to proctcted and the parent element is changed to public (), the following error will be reported when the access permission of the parent class is set to be greater than that of the child class.

Third, the parameter must be the same as the name. Specifically, the number of parameters must be the same as that of the parent class, rather than the same parameter name. That is, the name of the passed parameter can be any, as long as the number of passed parameters is the same.

The above describes two implementations of polymorphism in PHP.

Well, that's almost the case ..

Polymorphism. what is polymorphism? Polymorphism literally means multiple states. In the area...

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.