php (十三) 面向对象 封装
程序员文章站
2022-05-31 14:09:50
...
面向对象的封装性:
1,就是把对象的成员(属性,方法)结合成一个独立的相同单位,并尽可能隐藏对象的内部细节
public protected
private 私有的,用这个关键字修饰的成员,只能在对象内部访问(只有用$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$ageage=$age; } function getAge(){ if($this->ageage; }elseif($this->ageage-5; }elseif($this->ageage-10; }else{ return $this->age-15; } } function say(){ echo "我的名字是:".$this->name.",年龄是:".$this->age.",性别是:".$this->sex.'
'; } function __destruct(){ echo $this->name.",再见"."
"; } } $p1=new Person("rayhooo",26,"male"); $p1->say(); echo $p1->getPro("name").'
'; $p1->setAge(45); echo $p1->getAge().'
';
上一篇: mysql查询优化相关技巧