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; //此处将调用默认自动调用 __get()函数
以上就介绍了 php类中 __set 和 _get的用法示例,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。