<?php
/*定义员工类*/
class Employee{
var $id;
var $name;
var $birthday;
var $salary;
/*成员方法,设置员工号*/
function setId($id){
$this->id=$id;
}
/*成员方法,设置名字号*/
function setName($name){
$this->name=$name;
}
/*成员方法,设置生日号*/
function setBirthday($birthday){
$this->birthday=$birthday;
}
/*成员方法,设置工资号*/
function setSalary($salary){
$this->salary=$salary;
}
/*成员方法,获取员工号*/
function getId(){
return $this->id;
}
/*成员方法,获取员工名字*/
function getName(){
return $this->name;
}
/*成员方法,获取员工生日*/
function getBirthday(){
return $this->birthday;
}
/*成员方法,获取员工工资*/
function getSalary(){
return $this->salary;
}
function getEmployeeInfo(){
$info="员工号:".$this->getId()."|";
$info.="员工名字:".$this->getName()."|";
$info.="员工生日:".$this->getBirthday()."|";
$info.="员工工资:".$this->getSalary()."<br>";
return $info;
}
/*构造函数*/
function Employee($id,$name,$birthday,$salary){
$this->setId($id);
$this->setName($name);
$this->setBirthday($birthday);
$this->setSalary($salary);
}
}
/*定义项目经理的类*/
class ProjectManager extends Employee{
var $project_name;//负责项目名称
var $sub_employes=array();//职员名单
//构造函数
function ProjectManager($id,$name,$birthday,$salary){
$this->Employee($id,$name,$birthday,$salary);
}
//设置项目名称
function setProjectName($pjName){
$this->project_name=$pjName;
}
//获取项目名称
function getProjectName(){
return $this->project_name;
}
//增加新的组员
function addEmployee(&$employee){
$this->$sub_employes[]=& $employee;
}
function getEmployeeInfo(){
$info ="项目名:".$this->getProjectName()."<br>";
$info.="员工号:".$this->getId()."|";
$info.="员工名字:".$this->getName()."|";
$info.="员工生日:".$this->getBirthday()."|";
$info.="员工工资:".$this->getSalary()."<br>";
$info.="---------------------------------------------------------------<br>";
/*foreach($this->sub_employes as $value){
$info.=$value->getEmployeeInfo();
}*/
return $info;
}
}
$manager=new ProjectManager(2006100,"佟湘玉","1982-2-14",4000);
$manager->setProjectName("同服商场-php网络购物系统");
/*$manager->addEmployee($employee);
$manager->addEmployee(new Employee(2006099,"李大嘴","1978-3-23",3000));*/
echo $manager->getEmployeeInfo();
$employee=new Employee(2006190,"祝无双","1982-7-14",2000);
$employee2=new Employee(2006100,"佟湘玉","1982-5-14",4000);
echo $employee->getEmployeeInfo();
echo $employee2->getEmployeeInfo();
?>

解决方案 »

  1.   

    function addEmployee(&$employee){
    $this->$sub_employes[]=& $employee;
    }不明白你这个地方为什么用个引用.其中是把$employee的值付给这个引用,那这个引用是找不到这个值的地址的.而且$this->$sub_employes[]是不对的.$this->$sub_employes
      

  2.   

    因为$sub_employes[] 在前面定义的是个数组呀 
    不要这个$sub_employes[]会报错的呀