yii2知识点理解(成员属性)

yii2成员属性

  成员变量类似于public $a;

  成员属性类似于 public function a(){}

 

成员变量是就类的结构构成而言的概念,而属性是就类的功能逻辑而言的概念

 

成员属性应用:

  在components目录下创建post.php 内容为

<?php
namespace app\components;
use yii\base\Object;

/**
* Created by PhpStorm.
* User: yifan
* Date: 2016/8/25
* Time: 10:10
* 自定义实现属性
*/
class post extends Object{
private $_title;
public function setTitle($title){
$this->_title = $title;
}
public function getTitle(){
return $this->_title;
}
}

之后在配置文件
components里添加配置参数
'testpost'=>['class'=>'app\components\post','title'=>'dfgdfgdf111g'],

配置结束

使用方法:
  在控制器里面使用:
echo Yii::$app->testpost->getTitle();


 

posted on 2016-08-25 10:42  我很迷茫  阅读(222)  评论(0编辑  收藏  举报