封装性和魔术方法
2015-12-25 19:58
387 查看
<?php class person{ public $name; private $age; protected $sex; public function __construct($name, $age, $sex){ $this -> name = $name; $this -> age = $age; $this -> sex = $sex; } public function __set($name, $value){ if($name == 'age' && $value > 30){ $this -> $name = 10; }else{ $this -> $name = 5; } } public function __get($name){ if($name == 'sex'){ if($this -> $name == 'w'){ return "{$this -> name} is a woman!"; }else{ return "{$this -> name} is a man!"; } } return $this -> $name; } public function __isset($name){ if($name == 'age'){ return true; } } public function __unset($name){ unset($this -> $name); } } $person = new person("zhangsan", 18, 'w'); $person -> age = 20; //__set() echo $person -> age; //__get() echo "<hr />"; echo $person -> sex; //__get() echo "<hr />"; var_dump(isset($person -> name)); //isset()做出判断,true or false。如果不存在这个属性或存在属性但未初始化(NULL),就返回false。 var_dump(isset($person -> age)); //__isset() unset($person -> age); //__unset() echo "<hr />"; var_dump($person);
相关文章推荐
- sinh和asinh--双曲正弦和反双曲正弦函数
- 1008. 数组元素循环右移问题 (20)
- sin和asin--正弦和反正弦函数
- LeetCode 8 String to Integer (string转int)
- C++正则表达式的使用
- 公交线路管理 数据结构课程设计
- jdbc和Servlet简述
- 谈谈网站的设计利与弊
- ural 1072. Routing
- iOS项目中获取验证码倒计时及闪烁问题解决方案
- 黑马程序员-C语言-指针
- ping lwip网络协议栈 分析(一)
- NSURLSession的使用
- UITabBarController实现
- 地图与定位
- 4000 Android RotateAnimation详解
- rref--计算行阶梯矩阵
- orth--将矩阵正交规范化
- modem digital audio interface summary
- 找出斐波那契数