Blogger Information
Blog 31
fans 0
comment 0
visits 24386
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Day35-2018/1/10(__set(),__get(), 写个案例)
SmallKing的博客
Original
666 people have browsed it

(__set(),__get(), 写个案例)

<?php
class person //人类
{
    protected $name; //姓名
    private $age;//年龄
    private $sex;//性别
    public function __set($name,$value)//设置保护或私有属性
    {
        if($name=='age') {
            if ($value>0&&$value<120) {
                $this->age=$value;
            }else {echo  '非法输入年龄:'.$value;
            return; }
        }
        if($name=='sex') {
            if ($value=='男'||$value=='女') {
                $this->sex=$value;
            }else {echo  '请输入"男"或"女",性别不能为"'.$value.'"';
                return; }
        }

         $this->$name=$value;
    }
    public function __get($name)//获取商品信息
    {
        return $this->$name;
    }


}

$jj=new person;
$jj->name='小王';
echo $jj->name;
echo '<hr>';
$jj->age='啊';
echo $jj->age;
echo '<hr>';
$jj->sex='人妖';
echo $jj->sex;

QQ图片20180111190953.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post