免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1933 | 回复: 1
打印 上一主题 下一主题

php魔术方法: __get() 和 __set()的妙用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-11 18:24 |只看该作者 |倒序浏览
php魔术方法: __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. ?>
复制代码
上面是手册里的例子,但是我觉得应该是先判断类里面是否有处理属性的方法,有的话就调用该方法,没有就直接设置该属性。

论坛徽章:
0
2 [报告]
发表于 2012-03-11 22:20 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP