Home > Article > Backend Development > Examples of extension and inheritance usage of PHP classes_PHP tutorial
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:
?
2 3 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";<🎜> <🎜>?> |