Introduction of PHP5 polymorphism and dynamic binding _php skills

Source: Internet
Author: User

What is polymorphism?

Polymorphism is the third feature of object-oriented language after data abstraction and inheritance. In a literal sense, polymorphism means "multiple forms", and in simple terms, polymorphism is a feature of the ability to perform multiple forms, and in Oo it refers to the ability of a language to be handled differently according to the type of object, in particular the form of overloaded methods and inheriting classes. Polymorphism is considered to be an essential feature of object-oriented languages.

For example:

We create an interface Shape, define an empty method draw (), then all implementation classes must implement this method, assuming that Shape has two implementation classes: Triangle and Rectangle, although we cannot interpret PHP polymorphism through Java code like this:

Copy Code code as follows:

Shape s = new triangle ();
S.draw ();

However, a type hinting is introduced in PHP5.1, which restricts the parameter types of functions (or methods), and we use this feature to demonstrate the polymorphism of PHP5.

Refer to the following code:

Copy Code code as follows:

Class Testpolymorphism {
Public Function Drawnow (Shape $shape) {
$shape->draw ();
}
}

The parameter type passed in function Drawnow () must be an object of the shape interface derived class, where the argument we pass to Drawnow () may be an object of triangle or Rectangle, or it may be a derived class object of other shape interfaces, such as Cir CLE and so on, simply saying that the Drawnow () parameter type is unpredictable, and that the behavior of the $shape->draw () is ultimately determined by the specific type of parameters passed in, such as if the Triangle object is passed in, then the triangle () method is invoked, If the Rectangle object is passed in, the Rectangle draw () method is invoked. The behavior of a method that determines which object is invoked at run time based on the type of the object parameter being passed can be called polymorphism.

Shape can also be an abstract base class or a non-abstract base class, and the above argument is all set up. The difference is that the interface defines only a set of rules that an implementation class must follow, whereas a base class can provide some default behavior for derived classes.

The reference code is as follows:

Copy Code code 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::d raw () \ n";
}
}

/**
* Rectangle
*
* @uses Shape
* @version 1.0
* @copyright
*/
Class Rectangle implements Shape {
Public Function Draw () {
Print "Rectangle::d raw () \ 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 PHP5 Object pattern in haohappy translation is described in section Nineth:

In addition to restricting access, the access mode determines which method accesses the quilt class call or which property will be quilt class. function calls are associated with the function itself, and the relationship between member access and variable memory addresses, called bindings.

Another argument:

Binding (binding): The invocation of a method to the method itself is called a binding, when the binding occurs at compile time, is called static binding, and when the program is run, depending on the object's type, the binding method is determined to be dynamic bound.

PHP is a dynamic language that uses dynamic binding. There is no need to consider which binding strategy to take because it is all automatic.

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.