yii2:行为简单实例
yii2:行为
行为是 yiibaseBehavior 或其子类的实例。
就是:将一个类的属性和方法,赋给另一个类使用。
转自:http://www.cnblogs.com/acheng...
例如:
behavior
namespace app\components\behavior; use yii\base\Behavior; class MyBehavior extends Behavior{ 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; } }
然后controller中使用:
namespace app\modules\demo\controllers; use Yii; use app\models\DCountry; use yii\web\Controller; use app\components\behavior\MyBehavior; //use app\compon /** * Default controller for the `demo` module */ class DefaultController extends Controller { public $url; public function behaviors() { return [ // 匿名行为,只有行为类名 'MyBehavior'=>[ 'class'=>MyBehavior::className(), 'name'=>'jerry', 'age'=>20 ] ]; } /** * Renders the index view for the module * @return string */ public function actionIndex() { return $this->render('index', ['age'=>$this->age, 'name'=>$this->name]); } }
相关推荐
WasteLand 2020-10-18
Allinputs 2020-08-30
Ashes 2020-06-14
caiyiii 2020-06-14
kxguan 2020-06-14
daillo 2020-06-14
一粒沙里的世界 2020-06-14
ruxingli 2020-06-14
csssy00 2020-06-14
阿佐 2020-06-14
NameWFY 2020-05-28
NameWFY 2020-05-26
Robin罗兵 2020-05-16
caiyiii 2020-04-29
wmsjlihuan 2020-04-26
cbao 2020-04-26
csssy00 2020-04-19
igogo00 2020-03-09