PHP ã«ãããã¯ãã¼ã¸ã£ã®ç¨é
PHPでクロージャ
ä½ã£ã¦ã¿ããã®ã®ãå®ã¯ããã¾ãç¨éãæãæµ®ããã§ãªãã
ãªãã¨ãªãã array_walk ããã¼ã¯ã¼ãã«æµ®ããã§ã¯æ¥ããã©ã
ã¦ãã¨ã§ãã¨ãããã smarty ã§ä½¿ã£ã¦ã¿ããã¨æãã
CakePHP ã完全 smarty 化ãã¦ã¿ããã®ã®ã
smarty å
ã§ã®é
åã®æ±ããå¾®å¦ã ã
âäºã®é¡æ«ã¯ãã®è¾ºã«ã
http://d.hatena.ne.jp/am11op/20070614/1181837256
è¦ã¯ãphp ãªãããæ¸ãã¹ãã¨ããã
<?php echo $html->link('Edit', array('action'=>'edit', 2));?>
smarty ã§ã¯ view ã«å¤ãªã¡ã½ããä½ã£ãä¸ã§ããæ¸ãå¿ è¦ããã£ãã
{$html->link('Edit', $view->_array('action=>edit', 2))}
ã¾ããããã§ããããã ãã©ããã£ãããªã㧠closure (çãªãã®)ã使ã£ã¦ã¿ãã
Closure ã¯ãããªã®ã
class Closure { var $_args; var $_func; /** * @private */ function Closure($func=null, $args=null) { if ($func) { $this->_args = $args; $func = ereg_replace("^function\([^\)]*\) *\{", "", $func); $func = ereg_replace("\}$", "", $func); $this->_func = $func; } } /** * @param $func string closure * @param $arg1 $mixed first object/var which is used in $func * @param $arg2 $mixed second object/var which is used in $func * ... */ function bind() { $args = func_get_args(); return new Closure(array_shift($args), $args); } /** * use this method to call closure */ function call() { $func = create_function('$args', $this->_func); return $func($this->_args); } function func() { $args = func_get_args(); $obj = new Closure(array_shift($args), $args); return $obj->call(); } }
ç¡é§ã«ã¤ã³ã¹ã¿ã³ã¹éç£ãã¦ãã®ã¯ãæ¸ãç´ãã®ãé¢åã ã£ãããã
ãã®ã¯ã©ã¹ã closure ã¨ããåå㧠assign_by_ref ãã¦ãã£ã¦ã
smarty å
ãããã使ãã
{*$html->link('Edit', $view->_array('action=>edit', 2))*} {$html->link('Edit', $closure->func("return array('action'=>'edit', 2);"))}
æåæ°å¢ãã¦ãï¼
ãã¿ã§ãï¼
ã¾ããã¡ãã£ã¨å¤æ°ã php ã§å¦çãã¦ä½ãã®ã¡ã½ããã«æ¸¡ãããæã
ä¸åãã使ããªããã㪠modifier ããã¡ãã¡ä½ããªãã¦ããã®ã§ä¾¿å©ããã
ãã ããå
¥åå¤ã«å¯¾ãã¦ä½¿ãå ´åã¯ä»»æã®ã³ã¼ããå®è¡ãããªãããã«æ°ãã¤ãããã¨(æ£èªã¿)ã