Home > Backend Development > PHP Tutorial > Detailed explanation of polymorphism in PHP object-oriented development_PHP tutorial

Detailed explanation of polymorphism in PHP object-oriented development_PHP tutorial

WBOY
Release: 2016-07-13 10:50:18
Original
905 people have browsed it

This article will introduce you to the detailed explanation of polymorphism in PHP object-oriented development. I hope this tutorial will be helpful to you.

Polymorphism of classes

1. Introduction and advantages of polymorphism.
2. Operator: instanceof.
3. Simple application of polymorphism.

1. Introduction and advantages of polymorphism

Introduction: Polymorphism is the third feature of object-oriented languages ​​after abstraction and inheritance.

Example: USB interface, different functions will be used when plugging different things into it.

Advantages: OOP is not just a collection of many functions and functions, the purpose is to use classes, inheritance, and polymorphism to describe a situation in our lives.


2. Operator: instanceof

PHP is a type operator used to determine whether a given object comes from a specified object
Format:

The code is as follows Copy code
 代码如下 复制代码

class A {}
class B {}

$thing = new A;
if ($thing instanceof A) {
echo "A";
}
if ($thing instanceof B) {
echo "B";
}

class A {}

class B {}

$thing = new A;
 代码如下 复制代码

class A {

}

class B {

}

$new = new A;

if ($new instanceof A) {
echo "A";
}
if ($new instanceof B) {
echo "B";
}
?>

if ($thing instanceof A) {

echo "A";

}

if ($thing instanceof B) {

 代码如下 复制代码

interface MyUsb {
function type();
function alert();
}

class Zip implements MyUsb {
function type() {
echo "2.0";
}
function alert() {
echo "U盘驱动正在检测……
";
 }
}

class Mp3 implements MyUsb {
 function type() {
  echo "1.0";
 }
 function alert() {
  echo "MP3驱动正在检测……";
 }
}

class MyPc {
 function Add_Usb($what) {
  $what->type();
  $what->alert();
 }

}
$p = new MyPc();

$zip = new Zip();

$mp3 = new Mp3();

$p->Add_Usb($zip);
$p->Add_Usb($mp3);
?>

echo "B"; }
3. Simple application of polymorphism Example 1:
The code is as follows Copy code
class A {<🎜> <🎜>}<🎜> <🎜>class B {<🎜> <🎜>}<🎜> <🎜>$new = new A;<🎜> <🎜>if ($new instanceof A) {<🎜> echo "A";<🎜> }<🎜> if ($new instanceof B) {<🎜> echo "B";<🎜> }<🎜> ?>
Example 2:
The code is as follows Copy code
interface MyUsb {<🎜> function type();<🎜> function alert();<🎜> }<🎜> <🎜>class Zip implements MyUsb {<🎜> function type() {<🎜> echo "2.0";<🎜> }<🎜> function alert() {<🎜> echo "The USB drive is detecting...
"; } } class Mp3 implements MyUsb { function type() { echo "1.0"; } function alert() { echo "MP3 driver is detecting..."; } } class MyPc { function Add_Usb($what) { $what->type(); $what->alert(); } } $p = new MyPc(); $zip = new Zip(); $mp3 = new Mp3(); $p->Add_Usb($zip); $p->Add_Usb($mp3); ?>

Added an example 213.29.11.16 update




Inheritance and Polymorphism


/* Parent class */
class MyObject{
public $object_name; //Book name
public $object_price; //Book price
public $object_num; //Number of books
public $object_agio; //Book discount
function __construct($name,$price,$num,$agio){ //Constructor
$this -> object_name = $name;
$this -> object_price = $price;
$this -> object_num = $num;
$this -> object_agio = $agio;
}
function showMe(){ //Output function
The sentence echo ' will not be displayed. ';
}
}
/* Subclass Book */
class Book extends MyObject{ //Subclass of MyObject.
public $book_type; //Category
function __construct($type,$num){ //Declare the constructor method
$this -> book_type = $type;
$this -> object_num = $num;
}
function showMe(){ //Rewrite the showMe method in the parent class
return 'This time'.$this -> book_type.'Book'.$this->object_num.'this
';
}
}
/* Subclass Elec */
class Elec extends MyObject{ //Another subclass of MyObject
function showMe(){ //Rewrite the showMe method in the parent class
return 'Hot-selling books:'.$this -> object_name.'
Original price: '.$this -> object_price.'
Special price: '.$this -> object_price * $this -> ; object_agio;
}
}
/* Instantiate object */
$c_book = new Book('Computer class',1000); //Declare a Book subclass object
$h_elec = new Elec('PHP Function Reference Collection',98,3,0.8); //Declare an Elec subclass object
echo $c_book->showMe()."
"; //Output the showMe() method of Book subclass
echo $h_elec->showMe(); //The showMe() method that outputs the Elec subclass is
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632641.htmlTechArticleThis article will introduce you to the detailed explanation of polymorphism in PHP object-oriented development. I hope this tutorial will be useful to you. Classmates are helpful. Class polymorphism 1. Introduction and advantages of polymorphism. 2.Operators...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template