物件導向的封裝性:1,就是把對象的成員(屬性,方法)結合成一個獨立的相同單位,並儘可能隱藏對象的內部細節public protectedprivate 私人的,用這個關鍵字修飾的成員,只能在對象內部訪問(只有用$this訪問),不能在對象外部使用 樣本:
class Person{ private $name; private $age; private $sex; function __construct($name="",$age=20,$sex="male"){ $this->name=$name; $this->age=$age; $this->sex=$sex; } function getPro($name){ return $this->$name; } function setAge($age){ if($age>100$age<0){ return; } $this->age=$age; } function getAge(){ if($this->age<30){ return $this->age; }elseif($this->age<40){ return $this->age-5; }elseif($this->age<50){ return $this->age-10; }else{ return $this->age-15; } } function say(){ echo "我的名字是:".$this->name.",年齡是:".$this->age.",性別是:".$this->sex.'<br>'; } function __destruct(){ echo $this->name.",再見"."<br>"; } } $p1=new Person("rayhooo",26,"male"); $p1->say(); echo $p1->getPro("name").'<br>'; $p1->setAge(45); echo $p1->getAge().'<br>';