• 技术文章 >php教程 >php手册

    php (十三) 面向对象 封装

    2016-06-21 08:48:43原创628
    面向对象的封装性:

    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$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.'
    '; } function __destruct(){ echo $this->name.",再见"."
    "; } } $p1=new Person("rayhooo",26,"male"); $p1->say(); echo $p1->getPro("name").'
    '; $p1->setAge(45); echo $p1->getAge().'
    ';



    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:quot this age gt function
    上一篇:PHP面向对象中的重要知识点(三) 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • php批量添加数据与批量更新数据的实现方法,php添加数据• PHP之判断用户语言跳转网页• PHP 修改RAR文件注释及添加压缩文档讲解• header("Location:login.php")• PHP代码:Http断点续传的实现例子
    1/1

    PHP中文网