在php中不支持多重继承,如果我们向使用多个类的方法而实现代码重用有什么办法么?那就是组合。在一个类中去将另外一个类设置成属性。
下面的例子,模拟了多重继承。
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; // 工龄
10 public function getlengthofservice(){
11 return $this->lengthofservice;
12 }
13 }
14 // 上面的类中的set方法就不写了.
15 // 如果有个研究生,既是学生也算工龄.
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 ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool