PHP类中 __set() 和 _get()的用法示例

来源:互联网 发布:windows 日志备份 编辑:程序博客网 时间:2024/06/10 19:47
<?phpclass example {private $a;public function __set($a, $str) {//对输入进行验证。if (strlen($str) > 0) {return $this->$a = $str;} else {                        //如果不能通过验证则抛出错误                        throw new Exception( '字符串不能为空。');}}public function __get($a) {return 'The words you entered: ' . $this->$a;}}$c = new example();$c->a = 'Hello World!'; //此处将调用默认自动调用 __set()函数echo $c->a; //<pre name="code" class="php">此处将调用默认自动调用 __get()函数

0 0