é¢æ°å é¢æ°
PHPã®ããã¥ã¢ã«ã¿ã¦ããããããªã®ããã£ããä»ã®ä»ã¾ã§ããã®ãããå¢ãã§è¦éãã¦ãããã¾ããã ãã©ããç¥ãããã£ããªããããã§ãããã ããã³ã³ãã¤ã«è¨èªã£ã½ãæ¸ãæ¹ã ã£ã¦æ°ããã¦ããããphpã§ã§ããã¨ã¯æã£ã¦ãªãã£ãã*1
pascalï¼ã¨ãããDelphiè¨èª)ã§ãé¢æ°å é¢æ°ã¯ãã使ã£ã¦ãã
ã¡ã½ããå ã§åé·ã«ãªããããå¾ãªãç®æããããprivateã§å¤åºãããã»ã©ãã¬ãã·ãã«ãªå¦çã§ããªãã¨ãã«ãé¢æ°å é¢æ°ãå¤ãã«å½¹ã«ç«ã£ãï¼ãã¨ã¯å帰ã¨ãï¼ãã¯ãã¼ã¸ã£ãèªåã®ä¸ã§ã¯ãè¦ããã«é¢æ°å é¢æ°ã¿ãããªããã ãï¼ã£ã¦å§ãã¯ç解ãã¦ããï¼ã¨ããããã®ç解ã®ä»æ¹ã¯æªã å¤ãã£ã¦ããªãã®ã ãï¼ã
create_functionããããããã£ã¡ã®ã»ããããã¯å¥½ãã ãªãæãããæ¸ãæ¹ã ã£ãã®ã§æããã¡ã¢ãã
ãããããªæ°ã«ãªããããã¯ãã
å ¬å¼ãµã¤ãã®ããã¥ã¢ã«ã«è¼ã£ã¦ãããé¢æ°å é¢æ°ã§ããâ¦
http://www.php.net/manual/ja/language.functions.php#AEN4977以ä¸ã®ã¹ã¯ãªãããä½æãï¼/home/www/hoge.phpã¨ãã¾ãããï¼ã
');
}
bar();
}foo(); // â1åç®
foo(); // â2åç®
?>
å®è¡ãã¦ã¿ãã
$ php -v PHP 5.2.2 (cli) (built: May 30 2007 10:42:19) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies $ php hoge.php bar<br> Fatal error: Cannot redeclare bar() (previously declared in /home/admin/hoge.php:4) in /home/admin/hoge.php on line 3
ãã¡ãã¼ãå¤ãã£ã¦ãªããããããä»æ§ã£ã¦ãã¨ãªãããé¢æ°å é¢æ°ãå®ç¾©ããé¢æ°ã¯ãä¸åããå¼ã¹ãªãã£ã¦ãã¨ã«ãªããããã
ã ããããã¯PHPãããããã°åé¿å¯è½ã ããã ã
<?php function foo(){ if (!function_exists('bar')) { function bar(){ echo('bar<br>'); } } bar(); } foo(); // â1åç® foo(); // â2åç® ?>
ãããã¸ãã¯ã¾ãã«LLã£ã¦æãã ãªãè¦ãã¨ãããä¸è¨ã¯å®è¡çµæã
$ php hoge.php bar<br>bar<br>
ã ãã©ãLLã«ã¯ã¯ãã¼ã¸ã£ãããä¼¼åããphpãcreate_functionãããªãã¦ãã¯ãã¼ã¸ã£ãå®è£ ããã¹ãã ãperlã¿ãããä»£å ¥ or å®ç¾©ããã¨ãé¢æ°ã®ãã¤ã³ã¿ï¼ãªãã¡ã¬ã³ã¹ãï¼ãè¿ã£ã¦ãããããªã¤ãã
æãperlã§ã¯ãã¼ã¸ã£ã使ã£ãã¨ããã¹ã³ã¼ãã®é¢ä¿ã§ã¡ã¢ãªãªã¼ã¯èµ·ããããã¨ãã£ãããã©ãä»ã¨ãªã£ã¦ã¯æãããæã²åºã
(追è¨)
JavaScriptã¯å®ç§ã ã£ãã
<html> <head> <script> function foo(){ function bar(){ document.write('bar<br>'); } bar(); } foo(); foo(); </script> </head> </html>
*1:JavaScriptã§ãåæ§ãªæ¸ãæ¹ãã§ãã