我不知道我在哪里做错了。有人可以告诉我吗?__get和__set代码的问题
<?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
?>
虽然这两个魔术方法都得心应手保持记住很多人(包括我在内),不推荐的使用它们,因为它们[打破对象封装](http://en.wikipedia.org/wiki/Encapsulation_object-oriented_programming)。 – stefgosselin 2011-05-29 21:10:11
请参阅[这篇文章的正确使用示例](http://stackoverflow.com/questions/3634748/php-zend-say-avoid-magic-methods)尊重封装。 – stefgosselin 2011-05-29 21:17:32