phpclass实现动态属性

来源:互联网 发布:mac 预览 目录 编辑:程序博客网 时间:2024/04/28 12:17
  1. class membershipuser
  2. {
  3.     public $userid;
  4.     public $attributes;
  5.     
  6.     function __construct()
  7.     {
  8.         
  9.     }
  10.     function getuser($userid)
  11.     {
  12.         $this->userid = $userid;
  13.         $this->username = "123123"//这里的username可以用动态实现不需要定义
  14.     }
  15.     
  16.     function __get($name) { return $this->get($name); }
  17.     function __set($name$value) { return $this->set($name$value); } 
  18.     function __isset($name) { if ($this->$name!=""return true; else return false; }   
  19.     function __unset($name) { return $this->attributes[$name];}
  20.     function get($name) {if (isset($this->attributes[$name])) { return $this->attributes[$name];}}
  21.     function set($name,$values) { $this->attributes[$name] = $values;}
  22.     
  23. }
  24. $b = new membershipuser();
  25. $b->cc = "f1111";
  26. $b->dd = "d22222";
  27. echo($b->attributes['cc'].'<br/>');
  28. echo ($b->attributes['dd'].'<br/>');
  29. $b->getuser("fd");
  30. echo($b->username);