本帖最后由 steavenhe 于 2010-06-26 11:31:03 编辑

解决方案 »

  1.   

    class classModel
    {
      var $attribute;
      function __get($name)
      {
    echo"获取私有属性值调用__get()方法<br>";
         if(isset($this->$Deliver_name))
         {
     return ($this -> $name);
         }
         else
         {
            return(NULL);
         }
      }
      function __set($name,$value)
      {
        echo"设置私有属性值调用__set()方法<br>";
        $this -> $name = $value;
      }}
    $a = new classModel();
    $a -> attribute = 5;//__set()设置属性值
    $a -> attribute; //__get()检查属性值
      

  2.   

    试了一下楼主给出的代码,看上去很正常啊,跟楼主描述的不太一样。(难道是 PHP 版本问题?)我想楼主对于 __get() 和 __set() 的理解应该是正确的。我把楼主的代码稍微改了一下,供参考:
    class Test{
        private $name = '';
        private $pwd = '';    function __construct() {
            echo "hello"."<br />";
        }    function __set($property,$value) {
            if ( $property == 'name' )
                $this->name = $value;
            else if ( $property == 'pwd' )
                $this->pwd = $value;
        }    function __get($property) {
            if ( $property == 'name' )
                return $this->name;
            else if ( $property == 'pwd' )
                return $this->pwd;
        }
    }
      

  3.   

    //__get()方法用来获取私有属性
    private function__get($property_name)
    {
    if(isset($this->$property_name))
    {
    return($this->$property_name);
    }else{
    return(NULL);
    }
    }
    //__set()方法用来设置私有属性
    private function__set($property_name,$value)
    {
    $this->$property_name=$value;
    }
      

  4.   


    class classModel
    {
      var $attribute;
      function __get($name)
      {
    echo"获取私有属性值调用__get()方法<br>";
      if(isset($this->$name))
      {
    return ($this -> $name);
      }
      else
      {
      return(NULL);
      }
      }
      function __set($name,$value)
      {
      echo"设置私有属性值调用__set()方法<br>";
      $this -> $name = $value;
      }}
    $a = new classModel();
    $a -> attribute = 5;//__set()设置属性值
    $a -> attribute; //__get()检查属性值