php中parent的方法可用于调用父类中定义的成员方法,代码为【class Manager extends employee {public function getSal(){parent::getSal()}】。
本教程操作环境:windows7系统、PHP5.6版,DELL G3电脑,该方法适用于所有品牌电脑。
php中parent的方法:来引用父类的方法。parent:: 可用于调用父类中定义的成员方法。
parent::的追溯不仅于直接父类。
通过parent::调用父类方法
class employee{
protected $sal=3000;
public function getSal(){
$this->sal = $this->sal + 1200;
return $this->sal ;
}
}
class Manager extends employee {
//如果想让经理在员工工资的基础上多发1500元.
//必须先调用父类的getSal()方法.
public function getSal(){
parent::getSal();// 这里调用了父类的方法.
$this->sal = $this->sal + 1500;
return $this->sal ;
}
}
$emp = new employee();
echo "普通员工的工资是 " . $emp->getSal();
echo "
";
$manager = new Manager();
echo "经理的工资是: " . $manager->getSal();