PHP受保护的类和私有类什么区别

受保护的继承后可以访问,私有的只能在该类中访问,不会被继承访问
class Man{
protected $name='lee';//受保护
private $age=123;//私有
function __construct(){
echo $this->name;//lee
echo $this->age;//123

}

}
class Girl extends Man{
function __construct(){
echo $this->name;//lee
echo $this->age;//不会出现123,

}

}

new Man();

new Girl();


<?php
class Man{
    protected $name='lee';//受保护
    private    $age=123;//私有
    function __construct(){
        echo $this->name;//lee
        echo $this->age;//123

    }

}
class Girl extends Man{
    function __construct(){
        echo $this->name;//lee
        echo $this->age;//不会出现123,

    }

}

new Man();

new Girl();

posted @ 2016-09-20 10:52  fleam  阅读(3487)  评论(0编辑  收藏  举报