Blogger Information
Blog 14
fans 0
comment 1
visits 12728
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mvc与服务容器类
王珂
Original
755 people have browsed it

mvc与服务容器类

  1. <?php
  2. namespace mvc_demo;
  3. // 控制器依赖注入点改到构造方法, 实现对外部依赖对象的共享
  4. require 'UserM.php';
  5. require 'UserV.php';
  6. class Container1
  7. {
  8. // 对象容器
  9. protected $instances = [];
  10. // 绑定: 向对象容器中添加一个类实例
  11. public function bind($alias, \Closure $process)
  12. {
  13. $this->instances[$alias] = $process;
  14. }
  15. // 取出: 从容器中取出一个类实例 (new)
  16. public function make($alias,$params = [])
  17. {
  18. return call_user_func_array($this->instances[$alias],[]);
  19. }
  20. }
  21. $container = new Container1();
  22. $container->bind('model',function (){return new UserM;});
  23. $container->bind('view',function (){return new UserV;});
  24. class Contraller4
  25. {
  26. public function index(Container1 $container)
  27. {
  28. //获取数据
  29. $data = $container->make('model')->getData();
  30. //渲染模板
  31. return $container->make('view')->fatch($data);
  32. }
  33. public function __destruct(){
  34. }
  35. }
  36. //实例化控制器类
  37. $contraller = new Contraller4();
  38. echo $contraller->index($container);
  39. unset($contraller);
Correcting teacher:天蓬老师天蓬老师

Correction status:unqualified

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!