YII2框架controller的继承关系如下:
yii\base\components yii\base\controller yii\web\controller
而components源码里面的魔术方法让人印象深刻:
魔术方法:
是指某些情况下,会自动调用的方法,称为魔术方法
PHP面向对象中,提供了这几个魔术方法,
他们的特点 都是以双下划线__开头的
__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() 和 __clone()
常用魔术方法:'; } public function a() { return "this is a function..."; } public function __destruct() { echo __METHOD__ . ''; } public function __clone() { echo __METHOD__ . ''; } public function __call($func, $args) { echo "函数名:" . $func . ''; echo "参数:" . var_dump ( $args ) . ''; }}$test = new Test ();$clone = clone $test;echo $test->a ();var_dump ( $test->b ( '111', '222' ) );/**结果:Test::__constructTest::__clonethis is a function...函数名:barray (size=2) 0 => string '111' (length=3) 1 => string '222' (length=3)参数:nullTest::__destructTest::__destruct*/?>
魔术方法__get:
'; }}$lily = new Human();echo $lily->age;echo $lily->money;echo $lily->friend;echo '
';$lily->aaa = 111;$lily->bbb = 222;print_r($lily);/**结果:你想访问我的age属性你想访问我的money属性你想访问我的friend属性Human Object ( [money:Human:private] => 30两 [age:protected] => 23 [name] => lily [aaa] => 111 [bbb] => 222 )*/?>
魔术方法__isset__ 和 __unset__
'; return 1; } public function __unset($p) { echo '你想去掉我的', $p, '?!
'; }}$hua = new Dog ();if (isset ( $hua->leg )) {echo $hua->leg, '';}/*结果:4*/if (isset ( $hua->tail )) {echo '有tail属性';} else {echo '没有tail';}/*结果:你想判断我的tail属性存不存在有tail属性*//*** __isset() 方法, 当 用isset() 判断对象不可见的属性时(protected/private/不存在的属性) 会引发 __isset()来执行 问: isset($obj->xyz) 属性,为真, 能说明 类声明了一个xyz属性吗? 答:不能 ***/unset ( $hua->leg );print_r ( $hua );/*结果:Dog Object( [bone:protected] => 猪腿骨)*/unset ( $hua->tail );unset ( $hua->bone );/*结果:你想去掉我的tail?!你想去掉我的bone?!*/?>