thinkphp±Õ°üº¯ÊýÔõôÓÃ
thinkphpÊÇÒ»¸ö»ùÓÚmvcÉè¼ÆģʽµÄÇáÁ¿¼¶php¿ª·¢¿ò¼Ü£¬±»ÆÕ±éµØʹÓÃÓÚwebÓ¦ÓóÌÐòµÄ¿ª·¢¡£ÆäÖÐÉæ¼°µÄ±Õ°üº¯ÊýÊÇÒ»ÖÖÌØÊâµÄÄäÃûº¯Êý£¬Ëü¿ÉÒÔÔÚ³ÌÐòÔËÐÐʱ¶¯Ì¬µØ½¨Éè²¢Ö´ÐдúÂë¡£ÔÚthinkphpÖУ¬±Õ°üº¯ÊýµÄʹÓúÜÊÇÎÞа£¬¿ÉÒÔÓÃÓÚ¶àÖÖ³¡¾°¡£
±¾ÎĽ«ÏÈÈÝThinkPHPÖбհüº¯ÊýµÄ»ù±¾Ó÷¨£¬ÒÔ¼°ÔÚÏÖʵ¿ª·¢ÖеÄÓ¦Óó¡¾°ºÍ×¢ÖØÊÂÏî¡£
Ò»¡¢±Õ°üº¯ÊýµÄ½ç˵
±Õ°üº¯ÊýÊÇÒ»ÖÖÄäÃûº¯Êý£¬ËüûÖøÃû³Æ£¬µ«¿ÉÒÔ±»µ±×ö²ÎÊýת´ï¸øÆäËûº¯ÊýʹÓᣱհüº¯ÊýµÄ½ç˵·½·¨ÈçÏ£º
$Closure = function ($param) { // º¯ÊýÌå };
µÇ¼ºó¸´ÖÆ
ÆäÖÐ$ClosureÊDZհüº¯ÊýµÄÃû³Æ£¨¿ÉÒÔ×Ô½ç˵£©£¬$paramÊÇת´ï¸ø±Õ°üº¯ÊýµÄ²ÎÊý£¬º¯ÊýÌåÊÇÐèÒªÖ´ÐеĴúÂë¡£ÐèҪעÖصÄÊÇ£¬±Õ°üº¯Êýͨ³£±»½ç˵ÔÚº¯ÊýÄÚ²¿£¬Ò²¿ÉÒÔ×÷Ϊº¯ÊýµÄ²ÎÊýת´ï£¬²¢ÔÚ´úÂëÖ´ÐÐʱ±»¶¯Ì¬½¨ÉèºÍŲÓá£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
¶þ¡¢±Õ°üº¯ÊýµÄ»ù±¾Ó÷¨
±Õ°üº¯ÊýÒ»Ñùƽ³£ÓÃÓÚÐèÒª¶¯Ì¬½¨ÉèºÍŲÓú¯ÊýµÄ³¡¾°£¬ÀýÈçÔÚÊý×é²Ù×÷¡¢ÊÂÎñ´¥·¢¡¢»Øµ÷º¯ÊýµÈ³¡¾°ÖС£ÏÂÃæÊÇʹÓñհüº¯ÊýµÄһЩ»ù±¾Ó÷¨£º
Ö±½ÓŲÓñհüº¯Êý
ÏÂÃæµÄ´úÂëÑÝʾÁËÔõÑùÖ±½ÓŲÓñհüº¯Êý£º
$Closure = function ($param) { echo "Hello, ".$param."!"; }; $Closure("World"); // Êä³ö£ºHello, World!
µÇ¼ºó¸´ÖÆ
½«±Õ°üº¯Êý×÷Ϊ²ÎÊýת´ï
ÏÂÃæµÄ´úÂëÑÝʾÁËÔõÑù½«±Õ°üº¯Êý×÷Ϊ²ÎÊýת´ï£º
function array_map_c(Closure $func, array $arr) { $new_arr = array(); foreach($arr as $key => $value) { $new_arr[$key] = $func($value); } return $new_arr; } $arr = array(1, 2, 3); $new_arr = array_map_c(function($v) { return $v * 2; }, $arr); print_r($new_arr); // Êä³ö£ºArray ( [0] => 2 [1] => 4 [2] => 6 )
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öarray_map_c()º¯Êý£¬Ëü½ÓÊÜÒ»¸ö±Õ°üº¯Êý×÷Ϊ²ÎÊý£¬²¢½«Õâ¸ö±Õ°üº¯ÊýÓ¦Óõ½Êý×éµÄÿ¸öÔªËØÉÏ£¬·µ»ØÒ»¸öеÄÊý×é¡£
ÔÚÀàÖÐʹÓñհüº¯Êý
ÔÚÀàÖÐʹÓñհüº¯ÊýµÄÒªÁìºÍÔÚº¯ÊýÖÐʹÓÃÏàËÆ¡£ÏÂÃæµÄ´úÂëÑÝʾÁËÔõÑùÔÚÒ»¸öÀàÖÐʹÓñհüº¯Êý£º
class Person { private $name; public function __construct($name) { $this->name = $name; } public function sayHello() { $Closure = function () { echo "Hello, ".$this->name."!"; }; $Closure(); } } $John = new Person("John"); $John->sayHello(); // Êä³ö£ºHello, John!
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öPersonÀ࣬Ëü°üÀ¨Ë½ÓбäÁ¿$nameºÍÒ»¸ö¹«¹²ÒªÁìsayHello()¡£ÔÚsayHello()ÒªÁìÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö±Õ°üº¯Êý$Closure£¬ÓÃÓÚÊä³ö¡°Hello, John!¡±Õâ¸ö×Ö·û´®¡£ÐèҪעÖصÄÊÇ£¬ÔÚ$Closureº¯ÊýÖУ¬$this±äÁ¿¿ÉÒÔ»á¼ûÀàÖеÄ˽ÓбäÁ¿$name¡£
Èý¡¢±Õ°üº¯ÊýµÄÓ¦Óó¡¾°
±Õ°üº¯ÊýÔÚÏÖʵ¿ª·¢ÖпÉÒÔÓ¦ÓÃÓÚ¶àÖÖ³¡¾°£¬ÏÂÃæÊÇÆäÖеÄһЩ³£¼ûÓ¦Óó¡¾°£º
Êý¾Ý²Ù×÷
ÔÚ¾ÙÐÐÊý×é»òÊý¾Ý¼¯µÄ²Ù×÷ʱ£¬±Õ°üº¯Êý¿ÉÒÔ×÷Ϊһ¸ö»Øµ÷º¯Êýת´ï¸øÏà¹ØµÄº¯Êý£¬Äܹ»ÊµÏÖ¸üΪÎÞаµÄÊý¾Ý²Ù×÷¡£
ÊÂÎñ´¥·¢
ʹÓñհüº¯ÊýʵÏÖÊÂÎñ´¥·¢Ê±£¬¿ÉÒÔÔÚÊÂÎñ´¥·¢µÄʱ¼ä¶¯Ì¬µØ½¨ÉèºÍŲÓÃÏìÓ¦µÄÊÂÎñ´¦Àíº¯Êý¡£
ÑÓ³ÙÖ´ÐÐ
ʹÓñհüº¯Êý¿ÉÒÔʵÏÖÐèÒªÑÓ³ÙÖ´ÐеĴúÂë¿é£¬Ò²¿ÉÒÔÔÚÖ´ÐÐÍê±Ïºó·µ»ØÏà¶Ô½Ï´óµÄ¹¤¾ß£¬´Ó¶øïÔÌÇëÇó´ÎÊý£¬ÌáÉýÐÔÄÜ¡£
Êý¾ÝÑéÖ¤
ÔÚÊý¾ÝÑéÖ¤µÄʱ¼ä£¬±Õ°üº¯Êý¿ÉÒÔ×÷ΪÑéÖ¤Æ÷µÄ»Øµ÷º¯Êý£¬Æ¾Ö¤²î±ðµÄÐèÒªºÍ³¡¾°¶¯Ì¬µØ½¨ÉèÑéÖ¤¹æÔò¡£
ËÄ¡¢×¢ÖØÊÂÏî
ÔÚʹÓñհüº¯ÊýʱÐèҪעÖØһЩÎÊÌ⣬ÏÂÃæÊÇÆäÖÐһЩ³£¼ûµÄÎÊÌ⣺
1.±äÁ¿×÷ÓÃÓò
±Õ°üº¯ÊýºÍͨË׵ĺ¯ÊýÒ»Ñù£¬Ä¬ÈÏÇéÐÎϲ»¿ÉÖ±½Ó»á¼ûÍⲿ±äÁ¿¡£ÈôÊÇÐèÒª»á¼ûÍⲿ±äÁ¿£¬¿ÉÒÔʹÓÃuseÒªº¦×Ö½«Æäת´ï¸ø±Õ°üº¯Êý¡£
2.ÐÔÄÜÎÊÌâ
Ö»¹Ü±Õ°üº¯ÊýÔÚ²¿·Ö³¡¾°Ï¿ÉÒÔÌáÉý¿ª·¢Ð§ÂÊ£¬¿ÉÊÇÔÚÐÔÄÜÉÏËü»á±ÈͨË׺¯ÊýÂÔÂý¡£Òò´Ë£¬ÔÚÏÖʵ¿ª·¢ÖÐÐèҪƾ֤ÏêϸÇéÐÎÅжÏÊÇʹÓñհüº¯ÊýÕÕ¾ÉʹÓÃͨË׺¯Êý¡£
3.¼æÈÝÐÔÎÊÌâ
ÐèҪעÖصÄÊÇ£¬ÔÚijЩPHP°æ±¾ÖУ¬±Õ°üº¯Êý¿ÉÄܻᱣ´æ¼æÈÝÐÔÎÊÌâ¡£ÈôÊÇÔÚʹÓñհüº¯ÊýµÄÀú³ÌÖзºÆðÎÊÌ⣬¿ÉÒÔ¼ì²éPHPµÄ°æ±¾£¬²¢ÊµÑéÉý¼¶µ½×îеİ汾¡£
×ÜÖ®£¬±Õ°üº¯ÊýÊÇÒ»¸öºÜÊÇÎÞаºÍÇ¿Ê¢µÄ±à³Ì¹¤¾ß£¬Äܹ»ÔÚÐí¶à³¡¾°ÖÐÊ©Õ¹Ö÷Òª×÷Óá£ÔÚThinkPHPÖУ¬±Õ°üº¯ÊýµÄʹÓúÜÊÇÎÞа£¬¿ÉÒÔÓ¦ÓÃÓÚ¶àÖÖÊý¾Ý²Ù×÷¡¢ÊÂÎñ´¥·¢¡¢ÑÓ³ÙÖ´ÐС¢Êý¾ÝÑéÖ¤µÈ³¡¾°¡£Í¬Ê±Ò²ÐèҪעÖØһЩÎÊÌ⣬ÀýÈç±äÁ¿×÷ÓÃÓò¡¢ÐÔÄÜÎÊÌâ¡¢¼æÈÝÐÔÎÊÌâµÈ¡£ÕÆÎÕ±Õ°üº¯ÊýµÄÓ÷¨ºÍ×¢ÖØÊÂÏÓÐÖúÓÚÌá¸ß³ÌÐòµÄ¿É¶ÁÐÔ¡¢¿Éά»¤ÐÔºÍÐÔÄÜ¡£
ÒÔÉϾÍÊÇthinkphp±Õ°üº¯ÊýÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡