PHP polymorphic code examples_PHP tutorial

WBOY
Release: 2016-07-13 09:48:46
Original
995 people have browsed it

PHP polymorphic code examples

This article mainly introduces PHP polymorphic code examples. This article uses 2 code examples to demonstrate polymorphism in PHP. Friends who need it can For reference

Polymorphic definition: only care about an interface or base class, not the specific class of an object. (Same type, different results)

Here are two examples:

First, we found that the base class defines the standard and the subclass implements its own rules. This is a requirement of polymorphism. At the same time, this is to satisfy rewriting; in fact, it is a different performance of different classes; it does not strictly satisfy an interface or base class programming. Because when you call it, it is not stu->showGrade() but its own method;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

class stu{

 

public function showGrade(){

echo "base class";

}

 

}

 

 

class xiaomin extends stu{

 

public function showGrade(){

echo "is son show 80";

}

}

 

class xiaoli extends stu{

 

public function showGrade(){

echo "is son show 60";

}

}

 

function doit($obj){

 

if(get_class($obj) != "stu"){

$obj->showGrade();

}

 

}

 

doit(new xiaoli());

doit(new xiaomin());

1 2

3

4

5

6

7

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

interface animal{

public function voice();

}

 

class cat implements animal{

public function voice(){

echo "miao~~~
";

}

}

 

class dog implements animal{

public function voice(){

echo "wang ~~~
";

}

}

 

function dovoice(animal $obj){

$obj->voice();

}

 

 

dovoice(new dog());

dovoice(new cat());

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class stu{ public function showGrade(){ echo "base class"; } } class xiaomin extends stu{ public function showGrade(){ echo "is son show 80"; } } class xiaoli extends stu{ public function showGrade(){ echo "is son show 60"; } } function doit($obj){ if(get_class($obj) != "stu"){ $obj->showGrade(); } } doit(new xiaoli()); doit(new xiaomin());
The second example: the dovoice parameter specifies $obj as animal, and the awareness is to use the interface to accept the implementation class object. An upward transformation. This is consistent with the same type but different results. This is polymorphism; Actually in Java it will be animal a = new dog(); like this; because PHP is a typed language. There is no object conversion mechanism. ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 interface animal{ public function voice(); } class cat implements animal{ public function voice(){ echo "miao~~~
"; } } class dog implements animal{ public function voice(){ echo "wang ~~~
"; } } function dovoice(animal $obj){ $obj->voice(); } dovoice(new dog()); dovoice(new cat());

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1022061.htmlTechArticlePHP polymorphic code examples This article mainly introduces PHP polymorphic code examples. This article uses 2 code examples to Demonstrate polymorphism in PHP. Friends who need it can refer to the definition of polymorphism: only care about one...
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