Blogger Information
Blog 14
fans 0
comment 0
visits 8780
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
事件委托的应用
于星辉
Original
568 people have browsed it
  1. <?php
  2. /**
  3. * 事件委托:请求委托 访问类中不存在的成员方法时,会被 魔术方法拦截,把请求重写到别的类的成员方法来处理
  4. * 委托是指一个对象转发或者委托一个请求给另一个对象,被委托的一方替原先对象处理请求
  5. * 委托比继承更加灵活,父类与子类的关系是固定的,只能单继承,但是请求可以委托给多个对象
  6. */
  7. // 被委托的类
  8. class Base{
  9. public function write(...$args)
  10. {
  11. printf('调用的不存在方法%s(),参数列表有[% s]<br>',__METHOD__,implode(',',$args));
  12. }
  13. }
  14. class Work
  15. {
  16. protected $Base;
  17. public function __construct(Base $Base)
  18. {
  19. $this->Base = $Base;
  20. }
  21. public function __call($method, $arg)
  22. {
  23. $this->Base->$method(...$arg);
  24. }
  25. }
  26. $base = new Base;
  27. $work = new Work($base);
  28. $work->write(1,2.3,4);
  29. ?>
Correcting teacher:PHPzPHPz

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