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;
//}

解决方案 »

  1.   

    本帖最后由 xuzuning 于 2011-12-09 10:07:16 编辑
      

  2.   

    调用不存在的情况才会调用__set
    <?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;