Introduction to PHP5 polymorphism and dynamic binding, php5 polymorphism dynamic binding _ PHP Tutorial

Source: Internet
Author: User
Tags types of functions
Introduction to PHP5 polymorphism and dynamic binding, and dynamic binding of php5 polymorphism. Introduction to PHP5 polymorphism and dynamic binding. what is the dynamic binding of php5 polymorphism? Polymorphism is the third feature of object-oriented language after data abstraction and inheritance. Literally, PHP5 polymorphism and dynamic binding are introduced, and php5 polymorphism is dynamically bound.

What is polymorphism?

Polymorphism is the third feature of object-oriented language after data abstraction and inheritance. Literally, polymorphism means "multiple forms". Simply put, polymorphism is a feature that represents multiple forms, in OO, the language has the ability to process objects in different ways, especially the form of overload methods and inheritance classes. Polymorphism is considered an essential feature of object-oriented languages.

For example:

We create an interface Shape and define an empty method draw (). Therefore, all implementation classes must implement this method. assume that Shape has two implementation classes: Triangle and Rectangle, although we cannot interpret PHP polymorphism through Java code like this:
The code is as follows:
Shape s = new Triangle ();
S. draw ();

However, Type Hinting is introduced in PHP5.1 to limit the parameter types of functions (or methods). We use this feature to demonstrate the PHP5 polymorphism.

Refer to the following code:
The code is as follows:
Class TestPolymorphism {
Public function drawNow (Shape $ shape ){
$ Shape-> draw ();
}
}

The drawNow () function restricts that the input parameter type must be the object of the Shape interface derived class. here, the parameter passed to drawNow () may be the object of Triangle or Rectangle, it may also be a derived class object of other Shape interfaces, such as Circle. In short, the drawNow () parameter type is unpredictable. $ shape-> draw () the behavior of is ultimately determined by the specific type of the input parameter. for example, if a Triangle object is input, the draw () method of the Triangle is called. if a Rectangle object is input, call the draw () method of Rectangle. This kind of behavior that determines the method of the object to be called at runtime based on the type of the passed object parameter can be called polymorphism.

Shape can also be an abstract base class or a non-abstract base class. the above discussion is true. The difference is that the interface only defines a set of rules that must be followed by the implementation class, and the use of the base class can provide some default behavior for the derived class.

The reference code is as follows:
The code is as follows:
/**
* Shape Interface
*
* @ Version 1.0
* @ Copyright
*/
Interface Shape {
Public function draw ();
}

/**
* Triangle
*
* @ Uses Shape
* @ Version 1.0
* @ Copyright
*/
Class Triangle implements Shape {
Public function draw (){
Print "Triangle: draw () \ n ";
}
}

/**
* Rectangle
*
* @ Uses Shape
* @ Version 1.0
* @ Copyright
*/
Class Rectangle implements Shape {
Public function draw (){
Print "Rectangle: draw () \ n ";
}
}

/**
* Test Polymorphism
*
* @ Version 1.0
* @ Copyright
*/
Class TestPoly {
Public function drawNow (Shape $ shape ){
$ Shape-> draw ();
}
}


$ Test = new TestPoly ();
$ Test-> drawNow (new Triangle ());
$ Test-> drawNow (new Rectangle ());

/* Vim: set expandtab tabstop = 4 shiftwidth = 4 :*/

What is dynamic binding?

The ninth section of PHP5 Object Pattern translated by HaoHappy includes the following:

In addition to restricted access, the access method also determines which method calls the quilt class or which attribute accesses the quilt class. The association between function calls and functions and the relationship between member access and variable memory addresses is called binding.

Another statement:

Binding: connecting a method call to a method itself is called binding. when binding occurs during compilation, it is called static binding, when the program runs, the binding method is dynamically bound based on the object type.

PHP is a dynamic language that uses dynamic binding. You do not need to consider the binding policy, because it is both automatic.

What is polymorphism? Polymorphism is the third feature of object-oriented language after data abstraction and inheritance. Literally ,...

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.