【简单问题】类里 __set() __get() 不会被执行?!!
时间:2011-12-09
来源:互联网
PHP code
class className{ public $attribute; function __construct() { $this->attribute=123; } /*function __get($name) { echo "__get()done!"; return $this->$name++; } */ //这东西无效?! function __set($name,$value) { echo "__set()done!"; if(($name="attribute")&&($value>=0)&&($value<=100)) $this->$name=$value; return 1; } } $formValue=50000; //if($formValue!=null) //{ $a=new className(); echo "first".$a->attribute."<br/>"; if($a->attribute=$formValue) if($a->attribute==$formValue) echo "设置成功!"; $a->attribute."|";//按理说这样就可以直接输出属性值了,不是么???构造函数是没问题的。 echo "".$a->attribute; //}
作者: layerbase 发布时间: 2011-12-09
你的 attribute 属性时公共的,当然就不行了
__get、__set 只在属性不存在或为保护protected;或为私有private 时才会起作用
__get、__set 只在属性不存在或为保护protected;或为私有private 时才会起作用
作者: xuzuning 发布时间: 2011-12-09
调用不存在的情况才会调用__set
PHP code
PHP code
<?php class className{ public $attribute; function __construct() { $this->attribute=123; } /*function __get($name) { echo "__get()done!"; return $this->$name++; } */ //这东西无效?! function __set($name,$value) { echo "__set()done!"; // if(($name="attribute")&&($value>=0)&&($value<=100)) $this->$name=$value; return 1; } } $formValue=50; $a=new className(); echo "first".$a->attribute."<br/>"; if($a->attribute=$formValue) if($a->attribute==$formValue) echo "设置成功!"; $a->attribute."|"; echo "".$a->attribute; $a->a = 40; echo $a->a;
作者: ci1699 发布时间: 2011-12-09
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28