CakePHPã®Controller->__mergeVars()ã§componentsããã¼ã¸ããã¦ãªãï¼
CakePHP 1.3.0 ãå¦ç¿ä¸ã«ããããããããã°ããï¼ã¨ããã®ã«ã¯ã¾ã£ãã®ã§ãããã°ã«æ¸ãã¦èª°ãã®ã¢ããã¤ã¹ãæå¾
ãããã¨ããè¨äºã§ãã
ãã°ã£ã½ããªãã¨æãã¤ã¤ããCakePHPã¯æè¿ã¯ãããã°ããã§ãããããã°ãªã®ããèªåãä½ããééãã¦ãã®ã*1ã¡ãã£ã¨èªä¿¡ãæã¦ãªãã§ãã¾ãã
ç¶æ³èª¬æ
AppControllerã§ã³ã³ãã¼ãã³ããè¨å®ãããã親ã¯ã©ã¹ã§ããControllerã§è¨å®ããã¦ãã Session ã®è¨å®ãæ¶ãã¦ãã¾ã£ãã¨ããã®ãããã®åé¡ã§ãã
ãªãã¸ã§ã¯ãæåã«ãããä¸è¬çãªç¶æ¿ã«å ããCakePHP ã¯ãã³ã³ããã¼ã©ã§ä½¿ãã³ã³ãã¼ãã³ãããã«ãã¼ã¨ãã£ãç¹å¥ãªã¢ããªãã¥ã¼ãã§ãå°ãæ°ã®å©ããåä½ããã¾ããAppController ã®å¤æ°é åããåã®ã³ã³ããã¼ã©ã¯ã©ã¹ã®é åã¨ãã¼ã¸ãããã®ã§ãã
AppController ã¨ã¢ããªã±ã¼ã·ã§ã³ã®åã³ã³ããã¼ã©ã®å¤æ°ã®ãã¡ãCakePHP ããã¼ã¸ãããã®ã¯æ¬¡ã®éãã§ãã
$components
http://book.cakephp.org/ja/view/957/The-App-Controller
$helpers
$uses
CakeBookã«ããæ¸ãã¦ããã®ã§ãAppControllerã§è¨å®ãã¦ãããã¾ãäºãã¼ã¸ãã¦ãããããã«ãªã£ã¦ããã¯ããªãã§ããã»ã»ã»
<?php class AppController extends Controller{ var $components = array('Acl', 'Auth'); }
ãã®æ§ã«ãAppController ã§2ã¤ã®ã³ã³ãã¼ãã³ããè¨å®ãã¾ããã
å®éã«ã¯Cakebookã®ãã¥ã¼ããªã¢ã«ããã£ã¦ããã®ã§ http://book.cakephp.org/ja/view/1545/Preparing-to-Add-Auth ã®ä¸é¨ã«ããæ§ãªãAppControllerãä½æãã¦ãã¾ãã
var $components = array('Session');
http://github.com/cakephp/cakephp/blob/master/cake/libs/controller/controller.php#L224
Controllerã§ã¯ãã®æ§ã«ãSessionã³ã³ãã¼ãã³ããè¨å®ããã¦ã¾ãã
if ($var === 'components') {
http://github.com/cakephp/cakephp/blob/master/cake/libs/controller/controller.php#L460
$normal = Set::normalize($this->{$var});
$app = Set::normalize($appVars[$var]);
if ($app !== $normal) {
$this->{$var} = Set::merge($app, $normal);
}
å®éã«ãã¼ã¸ãããã¸ãã¯ã¯ã¨ããã¨ãController->__mergeVars()ã«æ¸ããã¦ã¾ãã
__mergeVars()ã®ãç¹ã«ãã®é¨åããã©ãããå®éã«ã³ã³ãã¼ãã³ãããã¼ã¸ãã¦ããæã®ããã§ãã
$appVars[$var] ã® $appVars ã¯404行目㧠get_class_vars('AppController') ã§åå¾ãã¦ãããAppControllerã®æ¹ã§ãã
ã¨ããäºã¯ã親ã¯ã©ã¹ã§ãã Controller ã®ã $this->{$var} ã§åå¾ãããã¨ãã¦ãäºã«ãªãã¾ãã
$var ã®ä¸ã¯ 'components' ãªã®ã§ $this->components ã¨ããäºã«ãªãã¾ãããããã ã¨ãã¾ãè¡ããªããããªæ°ããã¾ãã
åã¯ã©ã¹ã§ããAppControllerãããã«ãã®åã¯ã©ã¹ã§ãããã¢ããªã±ã¼ã·ã§ã³ç¨ã«ä½æããã³ã³ããã¼ã©ã¼*2ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æããå ´åã$this->components ã¯æ¢ã«é è½*3ããã¦ãã¦ã親ã¯ã©ã¹ã® $components ã¯åå¾ã§ããªãããããªããã¨æãã¾ãã
é è½ã®å®é¨ã³ã¼ã
<?php class ParentClass{ var $foo ='parent'; function call(){ return $this->foo; } } class ChildClass extends ParentClass{ var $foo = 'child'; } $parent = new ParentClass(); $child = new ChildClass(); var_dump($parent->call(), $child->call());
string(6) "parent" string(5) "child"
çµæãè¦ãã¨ãChildClass ã®ã¤ã³ã¹ã¿ã³ã¹ããã ã¨ããã¯ã $foo ã¯é è½ããã¦ã¾ãã
*1:ãããã¯ç°å¢ã®ããï¼
*2:UsersControllerã¨ã
*3:ãªã¼ãã¼ã©ã¤ãã ã¨æã£ã¦ããã©ãããªè¨äºãè¦ããã¾ãããã»ãã¨ããªï¼ãã¹ã¼ãã¼ã¯ã©ã¹ã§å®ç¾©ããã¦ããã¯ã©ã¹ã¡ã½ãããã¡ã³ãå¤æ°ããµãã¯ã©ã¹ã§åå®ç¾©ãããã¨ã¯é è½ã¨è¨ããããªã¼ãã¼ã©ã¤ãã¨ã¯åºå¥ããã¾ãããhttp://www.javaroad.jp/java_class8.htm