亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2022 | 回復(fù): 1
打印 上一主題 下一主題

php魔術(shù)方法: __get() 和 __set()的妙用 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2012-03-11 18:24 |只看該作者 |倒序瀏覽
php魔術(shù)方法: __get() 和 __set()的妙用








Php代碼
  1. 1.<?php   
  2. 2.class Post {   
  3. 3.  private $title;   
  4. 4.  private $content;   
  5. 5.  private $author;   
  6. 6.  private $comments;   
  7. 7.  
  8. 8.  private $_getters = array('title', 'content', 'author', 'comments');   
  9. 9.  private $_setters = array('title', 'content', 'author');   
  10. 10.     
  11. 11.  public function __get($property) {   
  12. 12.    if (in_array($property, $this->_setters)) {   
  13. 13.      return $this->$property;   
  14. 14.    }   
  15. 15.    else if (method_exists($this, '_get_' . $property))   
  16. 16.      return call_user_func(array($this, '_get_' . $property));   
  17. 17.    else if (in_array($property, $this->_getters) OR method_exists($this, '_set_' . $property))   
  18. 18.      throw new Exception('Property "' . $property . '" is write-only.');   
  19. 19.    else  
  20. 20.      throw new Exception('Property "' . $property . '" is not accessible.');   
  21. 21.  }   
  22. 22.  
  23. 23.  public function __set($property, $value) {   
  24. 24.    if (in_array($property, $this->_getters)) {   
  25. 25.      $this->$property = $value;   
  26. 26.    }   
  27. 27.    else if (method_exists($this, '_set_' . $property))   
  28. 28.      call_user_func(array($this, '_set_' . $property), $value);   
  29. 29.    else if (in_array($property, $this->_setters) OR method_exists($this, '_get_' . $property))   
  30. 30.      throw new Exception('Property "' . $property . '" is read-only.');   
  31. 31.    else  
  32. 32.      throw new Exception('Property "' . $property . '" is not accessible.');   
  33. 33.  }   
  34. 34.}   
  35. 35.?>   
  36. 36.  
  37. 37.This way the variables in the $_getters array can be read from the outside and the variables in the $_setters array can be modified from the outside, like this:   
  38. 38.  
  39. 39.<?php   
  40. 40.$post = new Post();   
  41. 41.$post->title = 'Hello, World';   
  42. 42.echo $post->title;   
  43. 43.  
  44. 44.// The following will throw an exception since $comments is read-only:   
  45. 45.$post->comments = 23;   
  46. 46.?>   
  47. 47.  
  48. 48.And in case you need a less generic getter or setter at some point, you can remove the variable from the $_getters or $_setters array and implement a method like:   
  49. 49.  
  50. 50.<?php   
  51. 51.private function _set_title($value) {   
  52. 52.  $this->title = str_replace('World', 'Universe', $value);   
  53. 53.}   
  54. 54.?>   
  55. 55.  
  56. 56.And from the outside the property could still be used with:   
  57. 57.  
  58. 58.<?php   
  59. 59.$post->title = 'Hello, World!';   
  60. 60.?>  
  61. <?php
  62. class Post {
  63.   private $title;
  64.   private $content;
  65.   private $author;
  66.   private $comments;

  67.   private $_getters = array('title', 'content', 'author', 'comments');
  68.   private $_setters = array('title', 'content', 'author');
  69.   
  70.   public function __get($property) {
  71.     if (in_array($property, $this->_setters)) {
  72.       return $this->$property;
  73.     }
  74.     else if (method_exists($this, '_get_' . $property))
  75.       return call_user_func(array($this, '_get_' . $property));
  76.     else if (in_array($property, $this->_getters) OR method_exists($this, '_set_' . $property))
  77.       throw new Exception('Property "' . $property . '" is write-only.');
  78.     else
  79.       throw new Exception('Property "' . $property . '" is not accessible.');
  80.   }

  81.   public function __set($property, $value) {
  82.     if (in_array($property, $this->_getters)) {
  83.       $this->$property = $value;
  84.     }
  85.     else if (method_exists($this, '_set_' . $property))
  86.       call_user_func(array($this, '_set_' . $property), $value);
  87.     else if (in_array($property, $this->_setters) OR method_exists($this, '_get_' . $property))
  88.       throw new Exception('Property "' . $property . '" is read-only.');
  89.     else
  90.       throw new Exception('Property "' . $property . '" is not accessible.');
  91.   }
  92. }
  93. ?>

  94. This way the variables in the $_getters array can be read from the outside and the variables in the $_setters array can be modified from the outside, like this:

  95. <?php
  96. $post = new Post();
  97. $post->title = 'Hello, World';
  98. echo $post->title;

  99. // The following will throw an exception since $comments is read-only:
  100. $post->comments = 23;
  101. ?>

  102. And in case you need a less generic getter or setter at some point, you can remove the variable from the $_getters or $_setters array and implement a method like:

  103. <?php
  104. private function _set_title($value) {
  105.   $this->title = str_replace('World', 'Universe', $value);
  106. }
  107. ?>

  108. And from the outside the property could still be used with:

  109. <?php
  110. $post->title = 'Hello, World!';
  111. ?>
復(fù)制代碼
上面是手冊里的例子,但是我覺得應(yīng)該是先判斷類里面是否有處理屬性的方法,有的話就調(diào)用該方法,沒有就直接設(shè)置該屬性。

論壇徽章:
0
2 [報告]
發(fā)表于 2012-03-11 22:20 |只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP