Blogger Information
Blog 29
fans 1
comment 0
visits 23142
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP重载:属性拦截器,构造函数
阿心
Original
823 people have browsed it

重载:访问拦截器

  1. <?php
  2. //重载:访问拦截器(属性,方法拦截器)
  3. //访问不存在或无权限属性/方法自动调用
  4. // 属性拦截器 __set() , __get(), __isset() ,__unset()
  5. //// 构造方法: __construct(), 是类的实例化过程中被自动调用, new 的时候
  6. //再次注意:构造函数不能是静态
  7. class Demo
  8. {
  9. private $brand;
  10. private $model;
  11. protected $price;
  12. public function __construct($brand,$model,$price)
  13. {
  14. $this->brand=$brand;
  15. $this->model=$model;
  16. $this->price=$price;
  17. }
  18. //创建属性拦截器
  19. public function __get($accept)
  20. {
  21. $attri='get'.ucfirst($accept);
  22. return method_exists($this,$attri) ? $this->$attri() : null;
  23. }
  24. private function getBrand()
  25. {
  26. return "品-牌:".mb_substr($this->brand,0,1).'*';
  27. }
  28. private function getModel()
  29. {
  30. return "<br>型-号:".substr($this->model,0,3).'*';
  31. }
  32. private function getPrice()
  33. {
  34. return "价-格:".$this->price;;
  35. }
  36. //设置拦截器
  37. public function __set($accept,$val)
  38. {
  39. $attri='set'.ucfirst($accept);
  40. return method_exists($this,$attri) ? $this->$attri($val):null;
  41. }
  42. private function setBrand($val)
  43. {
  44. $this->brand=trim($val);
  45. }
  46. private function setModel($val)
  47. {
  48. $this->model=trim($val);
  49. }
  50. private function setPrice($val)
  51. {
  52. $this->price=$val;
  53. // var_dump($val);
  54. }
  55. public function __isset($accept)
  56. {
  57. $attri='isset'.ucfirst($accept);
  58. return method_exists($this,$attri) ? $this->$attri() : null;
  59. }
  60. private function issetBrand()
  61. {
  62. return $this->brand;
  63. }
  64. private function issetModel()
  65. {
  66. return $this->model;
  67. }
  68. private function issetPrice()
  69. {
  70. return isset($this->price);
  71. }
  72. //删除拦截器
  73. public function __unset($accept)
  74. {
  75. if($accept==='brand')
  76. {
  77. $attri='set'.ucfirst($accept);
  78. if(method_exists($this,$attri)) return $this->$attri(null);
  79. }
  80. }
  81. }
  82. //属性拦截器
  83. $demo=new Demo('啊为','pro20','请 ->商——家');
  84. echo $demo->barnd;
  85. ?>

简单熟悉下PHP混写html代码

  1. <html>
  2. <body>
  3. <style>
  4. table{border:0px;cellspacing:2px;cellpadding:2px;background:#FF00FF;width:200px}
  5. TR{ BACKGROUND:#ccd}
  6. TD{padding:0;margin:0;text-align:center}
  7. </style>
  8. <table >
  9. <tr>
  10. <td style="background:#FFF">属性拦截器</td>
  11. </tr>
  12. <tr>
  13. <td><?php echo $demo->brand;?></td>
  14. </tr>
  15. <tr>
  16. <td><?php echo $demo->model;?></td>
  17. </tr>
  18. <tr>
  19. <td><?php echo $demo->price; ?></td>
  20. </tr>
  21. <?php
  22. $demo->brand="米米";
  23. $demo->model="Note 3";
  24. $demo->price="999";
  25. ?>
  26. <tr>
  27. <td style="background:#FFF">设置拦截器</td>
  28. </tr>
  29. <tr>
  30. <td><?php echo $demo->brand; ?></td>
  31. </tr>
  32. <tr>
  33. <td><?php echo $demo->model; ?></td>
  34. </tr>
  35. <tr>
  36. <td><?php echo $demo->price; ?></td>
  37. </tr>
  38. <tr>
  39. <td style="background:#FFF">属性检测拦截器</td>
  40. </tr>
  41. <tr>
  42. <td><?php echo isset($demo->brand) ? '品-牌:存在':'品-牌:不存在'; ?></td>
  43. </tr>
  44. <tr>
  45. <td><?php echo isset($demo->moddel) ? '型-号:存在':'型-号:不存在'; ?></td>
  46. </tr>
  47. <tr>
  48. <td><?php echo isset($demo->pricde) ? '价-格:存在':'价-格:不存在'; ?></td>
  49. </tr>
  50. <?php
  51. unset($demo->brand);
  52. ?>
  53. <tr>
  54. <td style="background:#FFF">删除属性截器</td>
  55. </tr>
  56. <tr>
  57. <td><?php echo $demo->brand; ?></td>
  58. </tr>
  59. <tr>
  60. <td><?php echo $demo->model; ?></td>
  61. </tr>
  62. <tr>
  63. <td><?php echo $demo->price; ?></td>
  64. </tr>
  65. </table>
  66. </body>
  67. </html>

总结:这重载实在太难,反复研究就是不知道这个为什么,那个又为什么!好了,现在有初步的认识了,不过还是不熟悉。最后销毁这个没有研究出来!!方法拦截器再研究另外在发,应该比这个属性拦截器要简单易懂一点!!!!!!!!!!!!###总结:这重载实在太难,反复研究就是不知道这个为什么,那个又为什么!好了,现在有初步的认识了,不过还是不熟悉。最后销毁这个没有研究出来!!方法拦截器再研究另外在发,应该比这个属性拦截器要简单易懂一点!!!!!!!!!!!!

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