Blogger Information
Blog 17
fans 1
comment 0
visits 20050
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 拦截器与后期静态绑定的综合示例
大A
Original
636 people have browsed it

案例介绍

下面是一个模拟狼人杀的综合案例,用构造方法建立一个对象,每次建立对象所有玩家的职业随机生成,然后利用拦截器限制修改删除和查看其他人的身份,只有当自己的身份是’先知’时才可以查看所有人的身份.
最后利用后期静态绑定以查询构造器的形式一步完成修改自己的身份为’先知’并查看所有人的身份.

代码

  1. <?php
  2. abstract class smode
  3. {
  4. public static function myclass($data)
  5. {
  6. return new static($data);
  7. }
  8. abstract protected function show();
  9. abstract protected function print_p($text);
  10. }
  11. class mode extends smode
  12. {
  13. private $id = ['狼人', '女巫', '平民', '先知', '猎人'];
  14. private $player = ['张三', '李四', '王五', '赵六', '钱七'];
  15. private $array;
  16. private $name;
  17. protected function print_p($text)
  18. {
  19. printf('<pre>%s</pre><hr>', print_r($text, true));
  20. }
  21. public function __construct($name)
  22. {
  23. shuffle($this->id);
  24. $this->array = array_combine($this->player, $this->id);
  25. $this->name = $name;
  26. }
  27. protected function show()
  28. {
  29. $key = array_search('先知', $this->array);
  30. if ($key == $this->name) :
  31. foreach ($this->array as $key => $value) {
  32. $b .= ($key . ':' . $value . '<br>');
  33. }
  34. echo $b.'<hr>';
  35. else :
  36. echo '你不是先知不能查看.<hr>';
  37. endif;
  38. }
  39. public function __set($name, $value)
  40. {
  41. if (in_array($name, $this->player) && $name === $this->name) :
  42. $this->array[$name] = $value;
  43. echo $name . '将自己的身份修改成' . $value . '<hr>';
  44. else :
  45. echo '您无权修改其他人身份.<hr>';
  46. endif;
  47. }
  48. public function __get($name)
  49. {
  50. if (in_array($name, $this->player) && $name === $this->name) :
  51. return $name . ' 的身份是:' . $this->array[$name] . '<hr>';
  52. else :
  53. return '您无权查看其他人身份.<hr>';
  54. endif;
  55. }
  56. public function __isset($name)
  57. {
  58. if (in_array($name, $this->player) && $name === $this->name) :
  59. return isset($this->array[$name]);
  60. endif;
  61. return false;
  62. }
  63. public function __unset($name)
  64. {
  65. if (in_array($name, $this->player) && $name === $this->name) :
  66. unset($this->array[$name]);
  67. echo '删除了' . $name . '的所有信息<hr>';
  68. else :
  69. echo '你无权删除其他人信息<hr>';
  70. endif;
  71. }
  72. public function __call($fun, $data=null)
  73. {
  74. if ($fun == 'show') $this->show();
  75. }
  76. public function setID($id)
  77. {
  78. if (in_array($this->name, $this->player)) :
  79. $this->array[$this->name] = $id;
  80. endif;
  81. return $this;
  82. }
  83. }
  84. $a = '张三';
  85. $b = '李四';
  86. echo '生成一个自己('.$a.')的身份<hr>';
  87. $mode = new mode($a);
  88. echo '查看自己的身份<br>';
  89. echo $mode->$a;
  90. echo '查看别人的身份<br>';
  91. echo $mode->$b;
  92. echo '查看所有人的身份<br>';
  93. $mode->show();
  94. echo '修改自己的身份<br>';
  95. $mode->$a = '先知';
  96. echo '修改别人的身份<br>';
  97. $mode->$b = '先知';
  98. echo '查看所有人的身份<br>';
  99. $mode->show();
  100. echo '删除自己的信息<br>';
  101. unset($mode->$a);
  102. echo '删除别人的信息<br>';
  103. unset($mode->$b);
  104. echo '查看所有人的身份(因为自己的信息被删除所以不能查看所有人)<br>';
  105. $mode->show();
  106. echo "将自己的身份修改成先知并查看所有人身份<br>";
  107. mode::myclass('张三')
  108. ->setID('先知')
  109. ->show();

运行结果

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:这个案例有意思
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!