服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - Yii框架学习笔记之应用组件操作示例

Yii框架学习笔记之应用组件操作示例

2021-09-16 16:07倾听岁月 PHP教程

这篇文章主要介绍了Yii框架学习笔记之应用组件操作,结合实例形式分析了Yii框架自定义组件的创建与使用相关操作技巧,需要的朋友可以参考下

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

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

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

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

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

在应用目录下创建 components 目录

组件 CutomComponent

接口类 app\components\CustomComponent\CustomComponent;

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

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

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

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

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

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

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

组件有什么作用?

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

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

原文链接:https://blog.csdn.net/u014559227/article/details/77498946

延伸 · 阅读

精彩推荐