phpclass實現動態屬性

  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);
發佈了45 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章