Home > Backend Development > PHP Tutorial > Examples of extension and inheritance usage of PHP classes_PHP tutorial

Examples of extension and inheritance usage of PHP classes_PHP tutorial

WBOY
Release: 2016-07-13 09:49:02
Original
794 people have browsed it

Extension and inheritance usage examples of php class

This article describes the extension and inheritance usage of php class through examples. Share it with everyone for your reference. The details are as follows:

 ?

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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

class Thread

{

var $topic; //帖子主题

var $body; //帖子内容

var $date; //帖子发布时间

var $author; //帖子作者

//函数Thread用于初始化变量等

function Thread()

{

//初始化变量

}

//函数Send用于提交新帖子

function Send()

{

//检测变量的合法性后执行插入操作将变量存储到数据库中

}

//函数Edit用于编辑帖子

function Edit()

{

//检测变量的合法性后执行更新操作将变量存储到数据库中

}

//函数Delete用于删除帖子

function Delete()

{

//检测作者的权限后将从数据库中将相关数据删除

}

}

class MainThread extends Thread

{

var $id; //帖子编号

var $board; //帖子所在讨论区

var $allowreply; //是否允许回复

//构造函数,用于初始化变量

function MainThread($id, $board, $allowreply)

{

//用于初始化变量

}

function Send()

{

//检测变量的合法性后执行插入操作将变量存储到数据库中

parent::Send(); //用于调用基类的Send函数

}

function Edit()

{

//检测变量的合法性后执行更新操作将变量存储到数据库中

parent::Edit(); //用于调用基类的Edit函数

}

}

$th = new Thread; //创建新对象

if ($th instanceof Thread) //如果对象$th是Thread类型的,则输出Yes

echo "Yes";

else

echo "No";

?>

1

2 3

45 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<🎜>class Thread<🎜> <🎜>{<🎜> <🎜>var $topic; //Post topic<🎜> <🎜>var $body; //Post content<🎜> <🎜>var $date; //Post publishing time<🎜> <🎜>var $author; //Post author<🎜> <🎜>//Function Thread is used to initialize variables, etc.<🎜> <🎜>function Thread()<🎜> <🎜>{<🎜> <🎜>//Initialize variables<🎜> <🎜>}<🎜> <🎜>//Function Send is used to submit new posts<🎜> <🎜>function Send()<🎜> <🎜>{<🎜> <🎜>//After checking the validity of the variable, perform the insertion operation and store the variable into the database<🎜> <🎜>}<🎜> <🎜>//Function Edit is used to edit posts<🎜> <🎜>function Edit()<🎜> <🎜>{<🎜> <🎜>//After checking the validity of the variable, perform an update operation and store the variable in the database<🎜> <🎜>}<🎜> <🎜>//Function Delete is used to delete posts<🎜> <🎜>function Delete()<🎜> <🎜>{<🎜> <🎜>//After detecting the author’s permissions, the relevant data will be deleted from the database<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>class MainThread extends Thread<🎜> <🎜>{<🎜> <🎜>var $id; //Post number<🎜> <🎜>var $board; //The discussion board where the post is located<🎜> <🎜>var $allowreply; //Whether to allow reply<🎜> <🎜>//Constructor, used to initialize variables<🎜> <🎜>function MainThread($id, $board, $allowreply)<🎜> <🎜>{<🎜> <🎜>//Used to initialize variables<🎜> <🎜>}<🎜> <🎜>function Send()<🎜> <🎜>{<🎜> <🎜>//After checking the validity of the variable, perform the insertion operation and store the variable into the database<🎜> <🎜>parent::Send(); //Used to call the Send function of the base class<🎜> <🎜>}<🎜> <🎜>function Edit()<🎜> <🎜>{<🎜> <🎜>//After detecting the validity of the variable, perform an update operation and store the variable in the database<🎜> <🎜>parent::Edit(); //Used to call the Edit function of the base class<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>$th = new Thread; //Create new object<🎜> <🎜>if ($th instanceof Thread) //If the object $th is of type Thread, output Yes<🎜> <🎜>echo "Yes";<🎜> <🎜>else<🎜> <🎜>echo "No";<🎜> <🎜>?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/1020292.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1020292.htmlTechArticle Examples of extension and inheritance usage of php class. This article describes the extension and inheritance usage of php class. Share it with everyone for your reference. The details are as follows: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...
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