经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
Yii框架学习笔记之应用组件操作示例
来源:jb51  时间:2019/11/13 11:36:36  对本文有异议

本文实例讲述了Yii框架学习笔记之应用组件操作。分享给大家供大家参考,具体如下:

所有的组件都应声明在config/web.php

  1. //组件声明在该数组下
  2. 'components'=>array(
  3. //自定义组件1 - 函数形式
  4. 'customComponent1' => function(){
  5. $custom = new app\components\CustomComponent\realization\CustomComponent1();
  6. $custom->setName('谭勇');
  7. $custom->setAge(22);
  8. return $custom;
  9. },
  10. //自定义组件2 - 数组形式
  11. 'customComponent2' => array(
  12. 'class' => 'app\components\CustomComponent\relazation\CustomComponent2'
  13. 'name' => '谭勇',
  14. 'age' => 22
  15. ),
  16. //自定义组件 - 字符串形式
  17. 'customComponent3' => 'app\components\CustomComponent\realization\CustomComponent3'
  18. ),
  19.  

如果只是在components 中声明了该组件,那么只有在首次调用的时候才会实例化这个组件,之后调用都会复用之前的实例。 如果你在bootstrap 数组中声明了这个组件,那么该组件会随着应用主体的创建而实例(也就是默认会被实例,而不是首次调用才会实例这个组件)。

  1. //默认加载customComponent1 和 customComponent2 组件
  2. 'bootstrap' => array(
  3. 'customComponent1','customComponent2'
  4. ),
  5.  

在应用目录下创建 components 目录

组件 CutomComponent

接口类 app\components\CustomComponent\CustomComponent;

  1. <?php
  2. namespace app\components\CustomComponent;
  3. interface CustomComponent
  4. {
  5. public function setName($name);
  6. public function setAge($age);
  7. public function getName();
  8. public function getAge();
  9. }
  10. ?>
  11.  

接口实现类 app\components\CustomComponent\realization\CustomComponent1

  1. <?php
  2. namespace app\components\CustomComponent\realization;
  3. use app\components\CustomComponent\CustomComponent;
  4. class CustomComponent1 implments CustomComponent
  5. {
  6. public $name='勇哥';
  7. public $age = '我的年龄';
  8. public function setName($name)
  9. {
  10. $this->name = $name;
  11. }
  12. public function getName()
  13. {
  14. return $this->name;
  15. }
  16. public function setAge($age)
  17. {
  18. $this->age = $age;
  19. }
  20. public function getAge()
  21. {
  22. return $this->age;
  23. }
  24. }
  25. ?>
  26.  

customComponent2,customComponent3 我们都让他们与customComponent1 具有相同的代码。 那么我们怎么去调用这些组件呢?

  1. namespace app\controllers\home;
  2. use Yii;
  3. use yii\web\Controller;
  4. class IndexController extends Controller
  5. {
  6. public function actionIndex()
  7. {
  8. //组件customComponent1
  9. echo Yii::$app->customComponent1->getName();
  10. //组件customComponent2
  11. echo Yii::$app->customComponent2->getName();
  12. //组件customComponent3
  13. echo Yii::$app->customComponent3->getName();
  14. }
  15. }
  16.  

然后回过头看数组形式、函数形式、字符串形式的组件

  1. //函数形式 - 这个很容易理解 实例化后设置属性值
  2. function(){
  3. $custom = new app\components\CustomComponent\realization\CustomComponent1();
  4. $custom->setName('谭勇');
  5. $custom->setAge(22);
  6. return $custom;
  7. },
  8. //数组形式 - 它会实例化这个组件 之后设置属性值 注意这里设置属性值的方法 和 函数不一样,它是 $custom->name = '谭勇' , $custom->age = 22
  9. array(
  10. 'class' => 'app\components\CustomComponent\relazation\CustomComponent2'
  11. 'name' => '谭勇',
  12. 'age' => 22
  13. ),
  14. //字符串形式 只知道会实例化这个组件,怎么注入属性值,这个不清楚支不支持
  15.  

组件有什么作用?

如果你理解Java spring mvc 那么就不难理解组件的作用 可以作为服务层,数据访问层等等

更多关于Yii相关内容感兴趣的读者可查看jb51专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号