In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of polymorphism in PHP object-oriented, which is very detailed and has certain reference value. Friends who are interested must read it!
What is polymorphism?
Polymorphism is the third feature of object-oriented language after database abstraction and inheritance. Polymorphism is a variety of forms, with the ability to express a variety of forms. In an object-oriented representation, it is handled differently depending on the type of object. Polymorphism allows each object to respond to a common message in a way that suits it. Polymorphism enhances the flexibility and reusability of the software.
For example, we create a doing () method that prints the class if it is a student and prints to go to work if it is a company employee.
Common practice
Use if to judge
/ * *
* PHP polymorphism
, /
/ / define student classes
Class student {
Public function cla () {
Echo "the student worker is in class!"
}
}
/ / define the employee class
Class office {
Public function Wor () {
Echo "staff are at work!"
}
}
/ / method of judging object type
Function doing ($obj) {
If ($obj instanceof student) {
$obj- > cla ()
} elseif ($obj instanceof office) {
$obj- > wor ()
} else {
Echo "there is no such object!"
}
}
Doing (new student ()); / / students are in class
Doing (new office ()); / / staff are at work
The output of the above results:
The students are in class.
The staff is at work.
The drawback of this common approach is that if there are many objects, then if..else.. Very long, inflexible.
Polymorphic practice
Define a public abstract method that all subclasses inherit.
/ * *
* PHP polymorphism
, /
/ / define a public class
Class pub {
Protected function working () {
Echo "this method needs to be overloaded in subclasses!"
}
}
/ / define the student class and inherit the public class pub
Class student extends pub {
Public function working () {
Echo "the student worker is in class!"
}
}
/ / define the employee class, which inherits the public class pub
Class office extends pub {
Public function working () {
Echo "staff are at work!"
}
}
/ / method of judging object type
Function doing ($obj) {
If ($obj instanceof pub) {
$obj- > working ()
} else {
Echo "there is no such object!"
}
}
Doing (new student ()); / / students are in class
Doing (new office ()); / / staff are at work
This is the characteristic of polymorphism, flexible reuse.
Other practices
From the point of view of the implementation of polymorphism, it is nothing more than standardizing a method that each class should overload the parent class, so as to achieve a unified effect. It is also possible for us to add a unified method when defining classes. Therefore, the above example can also be implemented as follows:
The copy code is as follows:
/ * *
* PHP polymorphism
* Qiongtai blog
, /
/ / define student classes
Class student {
/ / define a unified method pub
Public function pub () {
Echo "the student worker is in class!"
}
}
/ / define the employee class
Class office {
/ / define a unified method pub
Public function pub () {
Echo "staff are at work!"
}
}
/ / method of judging object type
Function doing ($obj) {
If ($obj) {
/ / call the unified method of the class
$obj- > pub ()
} else {
Echo 'does not have this object'
}
}
Doing (new student ()); / / students are in class
Doing (new office ()); / / staff are at work
Of course, the above examples do not show that polymorphism can be solved in this way. After all, polymorphism can effectively achieve flexible reuse in complex programming.
Polymorphism can also be understood as a way of programming, and the ultimate goal of programming is nothing more than flexibility, polymorphism, reuse and efficiency.
This is all the content of the article "sample Analysis of Polymorphism in PHP object-oriented". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.