您的位置:首页 > 其它

public、private、protected的区别

2016-06-16 17:08 302 查看
public 公共的   谁都可以用,也可以修改

protected 受保护的  只有自己和后代可以使用和修改

private 私有的  只有自己可以使用和修改


eg:


class MyTest{
public $name;
protected $sex;
private $birth;

public function __construct($name,$sex,$birth){
$this-> name = $name;
$this-> sex = $sex;
$this-> birth = $birth;
}

public function getBirth(){
return $this->birth;
}

public function setSex($sex){
if($sex==='男'||$sex==='女'){
$this->sex = $sex;
}else{
return null;
}
}

}

$test = new MyTest('李四','男',time());

echo $test->name;
echo $test->getBirth();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: