对属性进行了重写
class employee{
protected $sal=3000;
public function getSal(){
return $this->sal;
}
}
class Manager extends employee {
protected $sal=5000;
public function getParentSal(){
return parent::getSal();
}
}
$manager = new Manager();
var_dump($manager);
echo "PHP ".phpversion()."<br>";
echo $manager->getSal();
echo "<br>";
echo "parent's \$sal ".$manager->getParentSal();
object(Manager)[1]
protected 'sal' => int 5000
PHP 5.6.27
5000
parent's $sal 5000
class employee{
//把protected 变成 private
private $sal=3000;
// protected $sal=3000;
public function getSal(){
return $this->sal;
}
}
class Manager extends employee {
protected $sal=5000;
public function getParentSal(){
//这里返回的是父类的private属性.
return parent::getSal();
}
public function getsa(){
echo parent::getSal();
return $this->sal;
}
}
$manager = new Manager();
var_dump($manager);
echo "PHP ".phpversion()."<br>";
echo $manager->getSal();
echo "<br>";
echo "parent's \$sal ".$manager->getParentSal();
echo "<br>";
echo $manager->getsa();
此时对象拥有2个属性,当调用Manager类里面的就用protected类型的sal,调用父类里面的sal就用private.
object(Manager)[1]
protected 'sal' => int 5000
private 'sal' (employee) => int 3000
PHP 5.6.27
3000
parent's $sal 3000
3000
5000