In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "introduction to PHP5 Polymorphism and dynamic binding". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "introduction to PHP5 Polymorphism and dynamic binding".
What is polymorphism?
Polymorphism is the third feature of object-oriented languages after data abstraction and inheritance. Literally, polymorphism means "multiple forms". To put it simply, polymorphism is a feature with the ability to express multiple forms. In OO, it means that "language has the ability to deal with it in different ways according to the type of object, especially in the form of overloaded methods and inherited classes". Polymorphism is considered to be a necessary feature of object-oriented languages.
For example:
Let's create an interface Shape and define an empty method draw (), then all implementation classes must implement this method. Suppose Shape has two implementation classes: Triangle and Rectangle, although we can't interpret the polymorphism of PHP through Java code like this:
The copy code is as follows:
Shape s = new Triangle ()
S.draw ()
However, Type Hinting is introduced into PHP5.1 to limit the parameter types of functions (or methods), and we use this feature to demonstrate the polymorphism of PHP5.
Refer to the following code:
The copy code is as follows:
Class TestPolymorphism {
Public function drawNow (Shape $shape) {
$shape- > draw ()
}
}
The function drawNow () restricts that the parameter type passed in must be the object of the Shape interface derived class. Here, the parameter we pass to drawNow () may be the object of Triangle or Rectangle, or it may be the derived class object of other Shape interface, such as Circle and so on. To put it simply, the parameter type of drawNow () is unpredictable, and the behavior of $shape- > draw () is ultimately determined by the specific type of the passed parameter. For example, if the object of Triangle is passed in, then the draw () method of Triangle is called, and if the object of Rectangle is passed in, the draw () method of Rectangle is called. This behavior of deciding which object's method to call at run time based on the type of object parameters passed can be called polymorphism.
Shape can also be an abstract base class or a non-abstract base class, all of which are true. The difference is that the interface defines only a set of rules that the implementation class must follow, while using the base class provides some default behavior for the derived class.
The reference code is as follows:
The copy 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?
Section 9 of the PHP5 Object Pattern translated by HaoHappy introduces:
In addition to restricting access, the access method also determines which method will be called by the subclass or which attribute will be accessed by the subclass. The association between a function call and the function itself, and the relationship between member access and the memory address of a variable, is called binding.
There is another saying:
Binding: connecting the call of a method to the method itself is called binding, when binding occurs at compile time, it is called static binding, and when the program is running, the binding method becomes dynamic binding according to the type of object.
PHP is a dynamic language that uses dynamic binding. There is no need to consider the binding strategy, because it is all automatic.
At this point, I believe you have a deeper understanding of "introduction to PHP5 Polymorphism and dynamic binding". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.