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();


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM