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