【发布时间】:2011-05-29 21:00:07
【问题描述】:
我不知道我在哪里做错了。谁能给我看看?
<?php
class something
{
public $attr1;
private $attr2;
public function __get($name)
{
return $this->$name;
}
public function __set($name,$value)
{
$this->$name = $value." added something more";
}
}
$a = new something();
$a->$attr1 = "attr1";
$a->$attr2 = "attr2";
echo $a->$attr1; //what I would expect is attr1 as output
echo $a->$attr2; //what I would expect is attr2 added something more as output
?>
【问题讨论】:
-
虽然这两种神奇的方法很方便,但请记住很多人(包括我)不推荐使用这些方法,因为他们break object encapsulation。
-
参见this post for a proper usage example,它尊重封装。
标签: php