Ec (2); in php, multi-inheritance is not supported. If we use multiple classes to implement code reuse, is there any way? That is the combination. Set another class as an attribute in a class. The following example simulates multiple inheritance. Viewsourceprint? 01 & lt ;? & Nbsp; 02 classuser {& nbsp; 03 & nbsp; private $ name & quot; tom & qu script ec (2 ); script
In php, multi-inheritance is not supported. If we use multiple class methods to implement code reuse, is there any way? That is the combination. Set another class as an attribute in a class.
The following example simulates multiple inheritance.
View sourceprint? 01
02 class user {
03 private $ name = "tom ";
04 public function getname (){
05 return $ this-> name;
06}
07}
08 class teacher {
09 private $ lengthofservice = 5; // length of service
10 public function getlengthofservice (){
11 return $ this-> lengthofservice;
12}
13}
14 // The set Method in the above class will not be written.
15 // if you have a graduate student, both the student and the working age are counted.
16 class graduatestudent extends user {
17 private $ teacher;
18 public function _ construct (){
19 $ this-> teacher = new teacher ();
20}
21 public function getlengthofservice (){
22 return $ this-> teacher-> getlengthofservice ();
23}
24}
25 $ graduatestudent = new graduatestudent ();
26 echo "name is". $ graduatestudent-> getname ()."
";
27 echo "lengthofservice is". $ graduatestudent-> getlengthofservice ();
28
29?>
Homepage 1 2 last