php基礎語法10--面向對象的訪問控制

<?php
class os{

    public $a = 1; // 公有
    private $b =2; // 私有
    protected $c = 3; // 受保護

    public function eat(){

        echo "public func\n";
    }
    private function eat_1(){

        echo "private func\n";
    }
    protected function eat_2(){

        echo "protected func\n";
    }

    function get(){
        echo $this->a,"\n",$this->b,"\n",$this->c,"\n";
        $this->eat();
        $this->eat_1();
        $this->eat_2();
    }


}

$g = new os();
$g->a;
$g->get();
//$g->b; // 不能訪問私有屬性
//$g->c; // 不能訪問受保護屬性
$g->eat();
//$g->eat_1();// 不能訪問私有方法
//$g->eat_2();// 不能訪問受保護方法


class os_1 extends os{

}

$f = new os_1();
$f->a;
$f->get();
//$g->b; // 不能訪問私有屬性
//$g->c; // 不能訪問受保護屬性
$g->eat();
//$g->eat_1();// 不能訪問私有方法
//$g->eat_2();// 不能訪問受保護方法

?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章