Blogger Information
Blog 26
fans 0
comment 0
visits 18437
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
后期静态绑定与拦截器
雪~人胖胖
Original
633 people have browsed it

后期静态绑定

  1. abstract class A
  2. {
  3. //当实现类中2个函数功能相同时,可以写在抽象类中
  4. //抽象类不能实例化
  5. //将类的定义与类的调用分离开
  6. //用后期静态绑定,使用关键字static将self替换
  7. //self 始终与定义它的类绑定
  8. //static始终与调用它的类绑定
  9. public function write()
  10. {
  11. //return new self;
  12. return new static();
  13. }
  14. public static function amount($price,$sum)
  15. {
  16. return '价格是:'.$amount = $price * $sum;
  17. }
  18. public static function index($price,$sum)
  19. {
  20. //return self::amount($price,$sum);
  21. return static::amount($price,$sum);
  22. }
  23. }
  24. class Base extends A
  25. {
  26. // public function write():self
  27. // {
  28. // return new self();
  29. // }
  30. public static function amount($price, $sum)
  31. {
  32. return '优惠后价格是:'.$amount = $price * $sum *0.8;
  33. }
  34. }
  35. class C extends A
  36. {
  37. // public function write():self
  38. // {
  39. // return new self();
  40. // }
  41. }
  42. //当子类改写父类的方法 由于是self关键字,在调用index()时 还是绑定定义它的父类的方法,所以子类改写没有效果
  43. echo Base::index(100,2); //输出结果是 价格是:200
  44. //如果把class A中的index()方法中的self改成static,那么调用Base的index() 就与Base绑定
  45. //输出的结果是 优惠后价格是:160

构造方法

  1. <?php //构造方法:__construct(),在类的实例化中被调用,功能就是生成一个新对象;
  2. class Product
  3. {
  4. private $name;
  5. private $price;
  6. public function __construct($name,$price,$sum)
  7. {
  8. //1.生成一个新对象,类实例
  9. //2.初始化对象
  10. $this->name = $name;
  11. $this->price = $price;
  12. $this->sum = $sum;
  13. $this->write();
  14. //返回值是隐式返回 返回当前新实例
  15. }
  16. public function write()
  17. {
  18. $count = $this->price * $this->sum;
  19. echo "$this->name:数量为:$this->sum,单价:$this->price 元,总价为$count 元";
  20. }
  21. }
  22. $product = new Product('手机',500,6);

拦截器

1.属性拦截器

  1. //PHP重载:又称拦截器,分属性拦截器,方法拦截器
  2. //使用场景:当用户访问一个不存在或无权访问的属性或者方法时自动调用
  3. //属性拦截器:__get(),__set(),__unset(),__isset()
  4. class Person
  5. {
  6. private $name;
  7. private $age;
  8. private $profession=12;
  9. public function __construct($name,$age)
  10. {
  11. $this->name = $name;
  12. $this->age = $age;
  13. }
  14. //访问拦截器
  15. public function __get($property)
  16. {
  17. $methed = 'get'.ucfirst($property);
  18. return method_exists($this,$methed) ? $this->$methed() : '无权访问';
  19. }
  20. private function getName()
  21. {
  22. return $this->name;
  23. }
  24. private function getAge()
  25. {
  26. return $this->age;
  27. }
  28. private function getProfession()
  29. {
  30. return $this->profession;
  31. }
  32. //设置拦截器
  33. public function __set($property,$value)
  34. {
  35. $methed = 'set'.ucfirst($property);
  36. return method_exists($this,$methed) ? $this->$methed($value) : null;
  37. }
  38. private function setAge($value)
  39. {
  40. return $this->age = $value;
  41. }
  42. //属性检测拦截器
  43. public function __isset($property)
  44. {
  45. return $property === 'name' ? true :false;
  46. }
  47. //属性销毁拦截器
  48. public function __unset($property)
  49. {
  50. if ($property==='profession'){
  51. unset($this->profession);
  52. }
  53. }
  54. }
  55. $person = new Person('张三',35);
  56. echo $person->name; //输出张三
  57. echo $person->age; //输出35
  58. echo '<hr>';
  59. $person->age = '40';
  60. echo $person->age; //输出40
  61. echo isset($person->name) ?'存在':'不存在';
  62. unset($person->profession);
  63. echo $person->profession; //销毁了profession

2.方法拦截器

  1. //方法拦截器:__call(),__callstatic
  2. class Product
  3. {
  4. public function __call($name, $arguments)
  5. {
  6. if($name === 'write')
  7. if(is_int($arguments[0]))
  8. {
  9. $this->doForint();
  10. }elseif(is_string($arguments[0])){
  11. $this->doForstr();
  12. }
  13. }
  14. public function doForint()
  15. {
  16. echo __METHOD__.'方法';
  17. }
  18. public function doForstr()
  19. {
  20. echo __METHOD__.'方法';
  21. }
  22. }
  23. $product = new Product;
  24. echo $product->write('1'); //输出Product::doForstr方法

总结

这节课学习后期静态绑定与拦截器,后期静态绑定不再被解析为定义当前方法所在的类,而是可以在子类绑定。拦截器可以对用户的一些非法设置进行处理,带来不必要的麻烦。

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