Loving Coding & Visual Design
PHP5成员重载错误: Indirect modification of overloaded property has no effect
PHP5成员重载的时候
出现如下错误:
根据Google结果:
[27 Sep 2007 8:16pm UTC] brjann at gmail dot com
php magic __set and __get
解决办法是给__get()加上引用传递&__get,
试图帮助函数找到引用被绑定的不存在的变量arr。但是这里存在没有调用__set的问题。
我的办法是$arr转为公有化,放弃__set。
Class Foo{
private $arr;
public function __get($key) {
if(isset($this->arr[$key])){
return $this->arr[$key];
}
}
public function __set($key, $val) {
if(isset($this->arr[$key])){
$this->arr[$key] = $val;
}
}
}
$f = new Foo;
$f->test = 1;
出现如下错误:
Notice: Indirect modification of overloaded property Foo::$arr has no effect
根据Google结果:
[27 Sep 2007 8:16pm UTC] brjann at gmail dot com
It seems that declaring the getter as "public function &__get(){...}" does the trick. However, it took some googling to find.
php magic __set and __get
this one works as expected. Actually php interpreter seems to do same thing. Trying to get value if exists and change it. Problem is that when it takes this value there is no place to store it and rise this error. Trying to modify property in not existing array. Solution is to make this array available. Returning values by reference seems to work.
解决办法是给__get()加上引用传递&__get,
public function &__get($key) {
return $this->arr[$key];
}
$f->array['test'] = 1;
试图帮助函数找到引用被绑定的不存在的变量arr。但是这里存在没有调用__set的问题。
我的办法是$arr转为公有化,放弃__set。
public $arr;
$f->arr['test'] = 1;
最 近 文 章
- 设置Apache禁止通过IP访问服务器 - Wed, 12 May 2010 04:55:18 +0000
- Godaddy的.COM和.NET域名即将涨价 - Wed, 05 May 2010 04:04:55 +0000
- 六年了,总结一个英文网站的SEO之路 - Sun, 02 May 2010 11:27:16 +0000
- Discuz7的图片附件不能显示的问题 - Wed, 28 Apr 2010 15:51:31 +0000
- Red Hat Enterprise AS 4安装DirectAdmin - Wed, 14 Apr 2010 04:00:47 +0000
- Are you ready for WebGL? - Wed, 24 Feb 2010 06:04:03 +0000
- WebGL三维示例和Js版Box2D物理引擎 - Sun, 21 Feb 2010 03:58:44 +0000
- FLash与HTML5 - Fri, 05 Feb 2010 08:28:02 +0000
- QuickBox2D - Fri, 22 Jan 2010 09:46:02 +0000
- RewriteRule里正则表达式减号字符的顺序问题 - Wed, 20 Jan 2010 10:17:40 +0000