深入理解面向对象5
2018年8月21日魔术方法 __set() __get() 防止用户调用没有的属性,和创建没有的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php class Account { private $user = 1; private $pwd = 2; public function __set($name, $value) { $this->$name = $value; } public function __get($name) { $this->$name = '这个就是默认值'; } } $a = new Account(); echo $a->user; $a->name = 5; echo $a->name; echo $a->big; |