Introduction to PHP Polymorphism Learning notes and examples

Source: Internet
Author: User
Tags abstract numeric value


The concept of polymorphism, in the Java middle finger is the type of object the variable can point to, but a subclass of the variable declaration type. Once an object is created, its type is invariant and polymorphism is a variable.
In PHP5, the type of the variable is indeterminate, and a variable can point to any type of numeric value, string, object, resource, and so on. We cannot say that polymorphism in PHP5 is a variable.


We can only say that in PHP5, polymorphism is applied to the type hint position of the method parameter.
Any subclass object of a class can satisfy the type requirement of the current type as a type hint. All the classes that implement this interface can satisfy the requirement of the method parameter of the type hint of the interface type, so the polymorphism refers to the multiple forms of the same thing.

Look at the following code:

Abstract class a{
public abstract function ABC ();
}

Class AB extends a{
Public Function abc () {
Echo ' AB ';
}
}

Class AC extends a{
Public Function abc () {
Echo ' AC ';
}
}

Class t{
Public Function abc () {
Echo ' t '
}
}

Class client{
public static function call (A $obj) {
$obj->abc ();
}
}

Client::call (New AB ());//ab
Client::call (New AC ());//ac
Client::call (New T ());   The method for the error client class should be public static function call ($obj) {$obj->climb (); }

If we create a doing () method, if the students print the class, if the company staff print to work.

Common practice

Use if to judge

/**
* PHP polymorphism
* Jones Taiwan Blog
*/

Defining student Classes
Class student{
Public Function cla () {
echo "Student worker is in class!<br/>";
}
}

Define Employee class
Class office{
Public Function Wor () {
echo "Staff is working!<br/>";
}
}

Determining Object Type Methods
function doing ($obj) {
if ($obj instanceof student) {
$obj->cla ();
}elseif ($obj instanceof Office) {
$obj->wor ();
}else{
echo "doesn't have this object!" ";
}
}

Doing (new student ()); The students are having a class
Doing (new Office ()); The staff is at work.

The above results are output:

The students are having a class

The staff is at work.

This common method has a disadvantage, that is, if the object is a lot, then if ... else.. Very long, not flexible.

Polymorphism approach
Defines a public abstract method that inherits from all subclasses.

/**
* PHP polymorphism
* Jones Taiwan Blog
*/

Define a public class
Class pub{
protected function Working () {
echo "This method needs to be overloaded in subclasses!";
}
}

Define student class, Inherit public class pub
Class Student extends pub{
Public Function working () {
echo "Student worker is in class!<br/>";
}
}

Define employee class, Inherit public class pub
Class Office extends pub{
Public Function working () {
echo "Staff is working!<br/>";
}
}

Determining Object Type Methods
function doing ($obj) {
if ($obj instanceof Pub) {
$obj->working ();
}else{
echo "doesn't have this object!" ";
}
}

Doing (new student ()); The students are having a class
Doing (new Office ()); The staff is at work.

This is the characteristic of polymorphism, flexible reuse.


Other practices

From the way of the realization of polymorphism, it is simply a way to standardize each class to overload the parent class, so as to achieve uniform effect. When we define a class, it is also possible to add a unified approach. So the above examples can also be achieved:
/**
* PHP polymorphism
* Jones Taiwan Blog
*/

Defining student Classes
Class student{
Define a unified approach pub
Public Function Pub () {
echo "Student worker is in class!<br/>";
}
}

Define Employee class
Class office{
Define a unified approach pub
Public Function Pub () {
echo "Staff is working!<br/>";
}
}

Determining Object Type Methods
function doing ($obj) {
if ($obj) {
Calling a uniform method of a class
$obj->pub ();
}else{
Echo ' does not have this object ';
}
}

Doing (new student ()); The students are having a class
Doing (new Office ()); The staff is at work.

Of course, the above example does not show that polymorphism can be used in such a way, after all, polymorphism in the complex program design, it can effectively achieve the characteristics of flexible reuse.

Polymorphism can also be understood as a programming approach, and the ultimate goal of programming is simply: flexibility, polymorphism, reuse, and efficiency.

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.