博主信息
博文 39
粉丝 0
评论 0
访问量 39987
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP:静态绑定与拦截器
原创
947人浏览过

一、实例演示后期静态绑定的原理与实现

  1. <?php
  2. abstract class creat1
  3. {
  4. public static function creat()
  5. {
  6. return new static();
  7. }
  8. }
  9. // 工作类1
  10. class User extends creat1
  11. {
  12. // 把原来的方法迁移到父类
  13. }
  14. // 工作类2
  15. class Prod extends creat1
  16. {
  17. // 把原来的方法迁移到父类
  18. }
  19. $user = new User();
  20. var_dump($user);
  21. echo '<br>';
  22. $prod = new Prod();
  23. var_dump($prod);

实例效果

二、实例演示属性重载/拦截器的所有方法

  1. <?php
  2. // 实例演示属性重载/拦截器的所有方法
  3. // 访问: 包括二个操作:就是读和写, 或者称为: 查询和设置
  4. // 属性拦截器: __set(), __get(), __isset(), __unset()
  5. class Goods
  6. {
  7. private $name;
  8. private $price;
  9. private $tax = 0.06;
  10. public function __construct($name,$price)
  11. {
  12. $this->name = $name;
  13. $this->price = $price;
  14. }
  15. // 1. 属性查询拦截器
  16. public function __get($property)
  17. {
  18. // a.先事先约定一些方法专用于处理属性访问:取出方法的名称
  19. $method = 'get'.ucfirst($property);
  20. // b. 根据方法的名称,转发访问请求
  21. return method_exists($this , $method) ? $this->$method() : null;
  22. }
  23. private function getname()
  24. {
  25. return $this->name;
  26. }
  27. private function getprice()
  28. {
  29. return $this->price;
  30. }
  31. // 2. 属性设置拦截器
  32. public function __set($prop , $value )
  33. {
  34. $meth = 'set'. ucfirst($prop);
  35. return method_exists($this , $meth) ? $this->$meth($value) : null;
  36. }
  37. private function setname($value)
  38. {
  39. $this->name = $value;
  40. }
  41. private function setprice($value)
  42. {
  43. $this->price = $value;
  44. }
  45. // 3. 属性检测拦截器
  46. public function __isset($prop)
  47. {
  48. // $meth = 'isset'. ucfirst($prop);
  49. return $prop === 'name' ? isset($this->name) : false;
  50. }
  51. // 4. 属性销毁拦截器
  52. public function __unset($prop)
  53. {
  54. $meth = 'unset'. ucfirst($prop);
  55. return method_exists($this , $meth) ? $this->$meth() : null;
  56. }
  57. }
  58. // 客户端
  59. // 创建对象,并初始化
  60. $goods = new Goods('十年普洱茶',1999);
  61. echo $goods->name . '的价格:' . $goods->price,'元','<br>';
  62. $goods->name = '今年的普洱茶';
  63. $goods->price = 30;
  64. echo $goods->name . '的价格:' . $goods->price,'元','<br>';
  65. echo isset($goods->name) ? '货物存在;' : '货物不存在;' ;
  66. echo isset($goods->price) ? '价格存在' : '价格不存在' . '<br>';
  67. unset($goods->name);
  68. echo $goods->name,'<br>';
  69. unset($goods->price);
  70. echo $goods->price,'<br>';

实例效果

三、实例演示方法重载/拦截器的所有方法

  1. <?php
  2. class User
  3. {
  4. // 方法拦截器
  5. public function __call($name, $arguments)
  6. {
  7. printf('方法名: %s , 参数: [%s]',$name , implode(',', $arguments) );
  8. }
  9. // 静态方法拦截器
  10. public static function __callStatic($name, $arguments)
  11. {
  12. printf('方法名: %s , 参数: [%s]',$name , implode(',', $arguments) );
  13. }
  14. }
  15. // 客户端
  16. $user = new User();
  17. $user->demo(1,2,3);
  18. echo '<br>';
  19. $user::demo(6,7,8);

实例效果

总结:
1.后期静态绑定工作在静态继承上下文中,定义类和调用类的绑定分开。self与定义类绑定,static与调用类绑定。
2.属性重载/拦截器
当访问一个不存在或者无权限访问的属性时,自动调用拦截器。
set()、get()、isset()、unset()
虽然理解它的原理,但是实现的代码有点陌生,希望日后慢慢熟悉。
3.方法重载/拦截器
call()、callstatic()
比属性拦截更有用。

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:这些拦截器, 是有点难的
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学