dz̸PHP ±Õ°üÌØÐÔÔÚʵ¼ÊÓ¦ÓÃÖеÄÎÊÌâ

5年以前  |  阅读数:910 次  |  编程语言:PHP 

ßÀ£¬Æaʵ´o²¿ÖÇe¿oÏÂÊÇ¿ÉÒԵģ¬¶øÓÐÐ(C)½Ãae»¹ÊÇÁiÈËdz£µÄÀ§ÈÅ£¬ÏÂÃaeÂýÂýµÀÀ´¡£
ºÜ¶aÓiÑԵĶ¼Ìa¹(C)ÁË
dz£ÓÅÑźÍƯÁÁµÄ²Ù×÷Êý×eµÄ½¨¡£ÔÚÏÂÃaeµÄÀý×ÓÖУ¬»aʹÓà PHP5.3 ÒÔ¼°ÆaËuÓiÑÔÌa¹(C)µÄ±Õ°u¹¦ÄÜ£¬ÓÃÓÚչʾÈçºÎ¡°¿Í¹ÛµÄ¡±²Ù×÷µu´uÊý×e¡£
Òe×¢£ºÔ­ÎÄ×÷Õ߱ȽϻðÐÇ£¬ÎÒ²»Á˽a Groovy ÒÔ¼° Scala ÓiÑÔ£¬ËuÒÔÕaÀiÎÒ¼ÓÉÏ Javascript µÄʵÏÖ¡£
ÔÚ¿ªÊ¼Ö®Ç°ÏÈ˵Ã÷Ï£¬±¾Àý×Ó½o½oÊDzuÃ÷¹Ûµa£¬²¢Ã»Óп¼ÂÇÐÔÄܵÈÆaËu*½ÃaeµÄÒoËØ¡£

¡°»o±ÈÈý¼Ò¡±

Óøo¼oµ¥µÄÀý×Ó¿ªÊ¼£¬ÓÐÏÂÃae¸oÊý×e£º
$nums = array(10, 20, 30, 40);ÐeÒªÕÒ³oÊý×eÖдoÓÚ 15 µÄÏi¡£ÄÇô£¬²»¿¼ÂDZհuµÄÇe¿oÏ£¬ÎÒÃÇ»oÐi»aÕaÑuд£º
$res = array();foreach ($nums as $n) { if ($n > 15) { $res[] = $n; }}Èç¹uÓiÑÔ±¾ÉiÓбհuÖ§³ÖµÄ£¬ÄÇô»oÐi»aÕaÑuд£¨Groovy ÓiÑÔ£(C)
def res = nums.findAll { it > 15 }»oÕßʹÓà Scala ÓiÑÔ
val res = nums filter (_ > 15)Òe×¢£ºJavascript 1.6 µÄ»°»aÊÇÈçÏÂ
var res = nums.filter(function(c){return c > 15});ÒoΪѭ»²Ù×÷Òѱ»³eÏoÆðÀ´£¬ËuÒÔ¿ÉÒÔ¿´µ½ Groovy ¡¢Scala £¨ÒÔ¼° Javascript£(C) ¶¼ºÜƯÁÁµÃÓÃÒ»ÐоͿÉÒÔ¸a¶¨¡£
µ±È»£¬Èç¹uʹÓà PHP5.3 µÄ±Õ°u£¬Ò²¿ÉÒÔ×oµ½
$res = array_filter($nums, function($v) { return $v > 15; });PHP ÔÚÕa
½ÃaeʹÓÃÁË±È Scala ¸u¶aµÄ×Ö*u£¬µ«¶Ô±ÈÏÈÇ°µÄÀý×Ó£¬Ëu¸u¼o¶Ì²¢ÇÒÄܸuºÃµÃÔĶÁ¡£

˳±a˵Ï£¬ÉÏÃaeµÄ PHP ´uÂeʵ¼ÊÉÏÊÇʹÓÃÁË Lambda ½aÎoʽ£¬²¢²»ÊǸoÕaeÕýµÄ±Õ°u£¬Õa¸o ²¢²»ÊÇÎÒÃÇÄ¿Ç°¹Ø×¢µÄÖصa¡£Ïeϸ²uÊo PHP ±Õ°uÒÔ¼° Lambda ½aÎoʽµÄ×ÊÁÏ£¬¿ÉÒԲο¼ÕaÀi¡£
Ä¿Ç°¿´À´¸Ð¾o¶¼»¹²»´i£¬ÄÇôÎÒÃÇÔÙµÄÌaÄ¿Ôo¼ÓµaÄѶȣºÕÒµ½ËuÓдoÓÚ 15 µÄÏi£¬ È»ºo³ËÒÔ 2 ÔÙ¼ÓÉÏ×÷ÓÃÓoÖеĵÄij¸o±aÁ¿ÖµÒÔºoÔÙ*µ»Ø¡£

Groovy µÄʵÏÖ£º
def x = 1def res = nums .findAll { it > 15 } .collect { it 2 + x }Scala µÄʵÏÖ£º
val x = 1val res = nums filter ( > 15) map (
2 + x)Òe×¢£¬Javascript µÄʵÏÖ£º
var i = 1;var res = nums.filter(function(c){return c > 15}).map(function(c){return c 2 + i});ÒÔ¼° PHP£º
$x = 1;$res = array_map( function($v) use ($x) { return $v
2 + $x; }, array_filter( $nums, function($v) { return $v > 15; }));¹a´Ó´uÂeÁ¿½Ãae£¬ÏÖÔÚ¿´ÆðÀ´ PHP ÓeÆaËuÓiÑÔÓгoÈeÁË¡£ÏÈÅ׿ª´uÂe×ÖÃaeÉϱ¾Éi µÄÉoÃÀ²»Ì¸£¬ÉÏÃaeµÄ PHP ´uÂe»¹Óиo¶iÍaµÄÎÊÌa¡£
ÀýÈ磬Èç¹uÐeҪʹÓÃÊý×eµÄ¼u¶ø
ÇÖµ×÷±È½Ï£¬Ôoô°i£¿Êǵģ¬ÉÏÃaeµÄ´uÂe¾Í°i²»µ½ÁË¡£Í¬Ê±£¬´ÓÓi¨½Ç¶ÈÉÏ˵£¬ÉÏÃaeµÄ´uÂedz£ÄÑÒÔÔĶÁ¡£

µe±¹eÕae£¬Õaʱ»¹Êǵõ»ØÀÏÍÁµÄ˼ÂÈ¥½a¾oÎÊÌa£º
$x = 1;$res = array();foreach ($nums as $n) { if ($n > 15) { $res[] = $n
2 + $x; }}ºo£¬ÕaÑu¿´ÆðÀ´ÓÖºÜÇa³þÁË¡£µ«Õa¸oʱºoÄa»oÐiÓÖ»aÃÔ»oÁË£º¡°ÄÇ»¹Ï¹ÕÛÌÚɶ£¬Õa²»¾ÍÊǸoÊý×e²Ù×÷Â𣿡±¡£
Êǵģ¬ºÃÏ»¹ÔÚºoÍ¡£Õa¸oʱºo¸ÃÈà PHP µÄijÐ(C)¸ß¼¶ÌØÐÔ³o³¡£¬À´¸a¶¨Õa¿´ËÆÓÐ×Ô²ÐÇaÏo µÄ¡°ÎÞÁÄÎÊÌa¡±¡£

ArrayObject ¨C ¶ÔÊý×eµÄa×°
PHP Óиo³Æ×÷ SPL µÄ±e×¼¿a£¬ÆaÖаuº¬Á˸o½Ð×o ArrayObject µÄÀa£¬ËuÄÜÌa¹(C)¡°ÏñÊý×eÒ» Ñu²Ù×÷Àa¡±µÄ¹¦ÄÜ£¬ÀýÈç
$res = new ArrayObject(array(10, 20, 30, 40));foreach ($res as $v) { echo "$vn";}ArrayObject ÊǸoÄÚÖõÄÀa£¬ËuÒÔÄa¿ÉÒÔÏñÆaËuÀaÀa²Ù×÷Ò»Ñu
a×°Ëu¡£

Arr - °uÉÏÌÇÒÂ

¼ÈÈ»ÎÒÃÇÒѾ­ÓÐÁË ArrayObject ÒÔ¼°±Õ°uÕaÐ(C)ÌØÐÔ£¬ÎÒÃǾͿÉÒÔ¿ªÊ¼³¢ÊÔa×°Ëu£º
class Arr extends ArrayObject{ static function make($array) { return new self($array); } function map($func) { $res = new self(); foreach ($this as $k => $v) { $res[$k] = $func($k, $v); } return $res; } function filter($func) { $res = new self(); foreach ($this as $k => $v) { if ($func($k, $v)) { $res[$k] = $v; } } return $res; }}ºÃÁË£¬Íoʾa±¸¡£ÏÂÃaeÖØдµÄ PHP ´uÂe¾Í¿ÉÒÔ½a¾oÉÏÃaeÌaµ½µÄÎÊÌa£¬²¢ÇÒ¿´ÆðÀ´Ói
¨ÉÏ¡°²i ²»¶a¡±ÁË£º

$res = Arr::make($nums) ->filter(function($k, $v) { return $v > 15; }) ->map(function($k, $v) { return $v 2; });ÉÏÃaeµÄ´uÂeÓe´«Í³½Ê½Óкβ»Í¬ÄØ£¿Ê×ÏÈ£¬ËuÃÇ¿ÉÒԵݹe²¢ÐγÉ×÷ÓÃÁ´Ê½µÄµ÷Óã¬Òo´Ë¿ÉÒÔ Ìi¼Ó¸u¶aµÄÀaËƲÙ×÷¡£

ͬʱ£¬¿ÉÒÔͨ¹ý»Øµ÷µÄÁ½¸o²ÎÊýÖ±ð²Ù×÷Êý×eµÄ¼uÒÔ¼°ÖµÆaÏi - $k ¶ÔÓ¦¼uÒÔ¼° $v ¶ÔÓ¦Öµ ¡£ÕaʹµÃÎÒÃÇ¿ÉÒÔÔÚ±Õ°uÖÐʹÓüuÖµ£¬ÕaÔÚ´«Í³µÄ PHP º¯Êý array_fliter ÖÐÊÇÎިʵÏֵġ£
ÁiÍa¸o´øÀ´µÄ¶iÍaºÃ´¦¾ÍÊǸu¼ÓÒ»Ö API µ÷Óá£Ê¹Óô«Í³µÄ PHP º¯Êý²Ù×÷£¬ËuÃÇÓпÉÄܵÚÒ»¸o²ÎÊýÊǸo±Õ°u£¬»oÕßÊǸoÊý×e£¬ÒÖ»oÊǶa¸oÊý×e¡­×Ü֮˭֪µÀÄØ£¿
ÕaÀiÊÇ Arr ÀaµÄÍeÕuÔ´´uÂe£¬»¹°uº¬ÁËÆaËuÓÐÓõĺ¯Êý£¨ÀaËÆ reduce ÒÔ¼° walk£(C)£¬ÆaʵËu ÃǵÄʵÏÖÆaʵ*½Ê½ºÍ´uÂeÀaËÆ¡£

²(C)ÞÄ

Õa¸oÎÊÌaÆaʵºÜÄѻشð - ÕaÐeÒª¸u¾Ý´uÂeµÄÉÏÏÂÎÄÒÔ¼°³ÌÐoÔ±×ÔÉiµÈÖÚ¶aÒoËؾo¶¨¡£Æaʵ £¬µ±ÎÒµÚÒ»ÑÛ¿´¼u PHP µÄ±Õ°uʵÏÖʱ£¬ÎҸоoËƺo»Øµ½ÁËÄǺܾÃÒÔÇ°µÄ Java ʱÆÚ£¬µ±Ê± ÎÒÔÚ¿ªÊ¼Ê¹ÓÃÄaÃuÄÚÖÃÀa£¨anonymous inner classes£(C)À´ÊµÏÖ±Õ°u¡£µ±È»£¬ÕaËaÈ»¿ÉÒÔ×oµ½£¬ µ«¿´ÆðÀ´ÊµÔÚÊÇÐ(C)»­ÉßÌi×a¡£PHP ±Õ°u±¾ÉiÊÇû´i£¬Ö»ÊÇËuµÄʵÏÖÒÔ¼°Ói¨ÈÃÎҸе½Ç³£µÄÀ§»o¡£
ÆaËu¾ßÓбհuÌØÐÔµÄÓiÑÔ£¬ËuÃÇ¿ÉÒÔdz£½±aµÄµ÷Óñհu²¢Í¬Ê±¾ßÓÐÓÅÑŵÄÓi¨¡£ÔÚÉÏÃaeµÄÀý×Ó ÖУ¬ÔÚ Scala ÖÐʹÓô«Í³µÄÑ­»Ò²¿ÉÒÔ¹¤×÷£¬µ«Äa»aÕaÑuдÂ𣿶ø´ÓÁi¸o½Ãae£¬ÄÇôÓÐÈË ËµÉÏÃaeÕa¸oÌaĿʹÓà PHP µÄ±Õ°uÒ²¿ÉÒÔʵÏÖ£¬µ«Ò»°aÇe¿oÏÂÄa»aÕaÑuдÂð£¿
¿ÉÒÔÈ
¶¨£¬PHP ±Õ°uÔÚÐ(C)Çe¿oÏ¿ÉÒÔ³ÉΪÈñÀuµÄ¾uµ¶£¨ÀýÈçÑÓʱִÐÐÒÔ¼°×ÊÔ´µ÷ÓýÃae£(C)£¬ µ«ÔÚ´«Í³µÄµu´uÒÔ¼°Êý×e²Ù×÷ÃaeÇ°¾ÍÏÔµÃÓÐÐ(C)ΪÄÑ¡£²»ÒªÆøÄÙ²»¹ÜÔoôÑu£¬ µe±¹eÕae±aд¾ßÓмaeÈÝÐԵġ¢ÇaˬµÄ´uÂeÒÔ¼° API ÊÇ×iÖØÒªµÄ¡£

½aÊøÓi

ÏñËuÓкoÀ´¼ÓÉϵÄÓi¨ÌØÐÔÒ»Ñu£¨¼ÇµÃµ±Äe Java µÄ Generics ÌØÐÔ²»£¿ÒÔ¼°Ç°¼¸ÄeµÄ PHP OOP ÌØÐÔ£(C)£¬ËuÃǶ¼ÐeҪʱ¼aÄ¥ºÏÒÔ¼°×iÖÕÎȶ¨ÏÂÀ´¡£Ëae×Å PHP5.3 ÉoÖÁ½«À´µÄ PHP6 Öð½¥ÆÕ¼°£¬Ô½À´Ô½¶aµÄ¼¼ÇɺÍÌØÐÔÏaÐÅÔÚ²»Ô¶µÄ½«À´Ö𽥵ı»´ÏÃ÷µÄ³ÌÐoÔ±ÍÚ¾o³oÀ´¡£
»Øµ½×i³oÎÄÕ¿ªÍ
ÄǸoÌaÄ¿£¬¶Ô±È
$res = Arr::make($nums) ->filter(function($k, $v) { return $v > 15; }) ->map(function($k, $v) { return $v 2; });ÒÔ¼°
val res = nums filter ( > 15) map (
2)Á½ÕßÖ®¼aµÄÇø±ð¡£¹e¸u½aµ×ËuÃǽoÊÇÓi*¨¶øÒÑ£¬±¾ÖÊÉ϶¼ÊÇÊa;ͬ¹e½a¾oÁËͬ¸oÎÊÌa¡£³ÌÐo ÓiÑÔµÄÓ¦ÓÃÌØÐÔ²»Í¬£¬×ÔÈ»ÊeÓÅÊeÁÓÒ²¾ÍÎ޴ӱȽϡ£
×iºo£¬ÕaÀiÓдËƪÎÄÕµĴuÂeʾÀý£¬ ÏaÐÅ¿ÉÒÔÕÒµ½¸u¶aÈçºÎʹÓà PHP ½øÐк¯Êýʽµu´u£¨µ±È»²»½o½oÊÇÕaÐ(C)£(C)µÄÐĵá£

²»¿¿Æ×Ö®²(C)Ö÷ÐĵÃ
̹°×½²£¬ËaÈ»ÔÚ PHP5.0 ֮ǰ¾ÍÁ˽a¹ýÌa³oµÄÐÂÔo±Õ°uµÈ¹¦ÄÜ£¬µ«ÔÚ¿´µ½ PHP5.3 Ìa¹(C)µÄ±Õ °uÒÔ¼° Lambda ¹¦Äܺo£¬ÓeÔ­±¾ÐÄÀiÆÚ´ýµÄ»¹ÊÇÓÐÐ(C)³oÈe¡£
ÉoÖÁÏa¶ÔÓÚÊiϤµÄ JavaScript£¬PHP µÄ±Õ°uÔÚÎÒ¿´À´£¬ÏñÊÇ¡°±ðµÄÓiÑÔ¶¼ÓÐÁË£¬ËuÒÔÎÒÒ²ÒªÓС± µÄÕaÖÖÐÄ̬ϵIJuÎi¡£

µ«ÕýÈçÉÏÎÄÖÐËuÑÔ£¬Ïa±È JavaScript µÈÆaËu¶¯Ì¬ÓiÑÔ£¬PHP ³oÓÚ×ÔÉiµÄÓ¦ÓÃÒÔ¼°ÊµÏÖµÄÕÜѧ ³o¢£¬ÓeÆaËu¿ª¢ÓiÑÔ²»¾¡Ïaͬ¡£

Òo´ËÔÚijÐ(C)ÌØÐԵĵ÷Óýʽ¡¢ÊµÏÖ½*¨Ò²»a²»Ò»Ñu£¬ÕaÄÑÃa»aÈÃÊiϤÁiÍa¾ßÓÐÀaËƹ¦ÄܵÄÓiÑÔ µÄÈ˸е½µÄ²»ÊÊÓ¦¡£

´Ó PHP5.3 ÍƳoÖÁ½ñ£¬»¹²»µ½°eÄeµÄʱ¼a£¬Ïa±È JavaScript µÈÕaÐ(C)ÔçÒѾßÓбհuµÈÌØÐ﵀ ¶¯Ì¬ÓiÑÔÏa±È£¬×ÔÈ»ÊÇÏÔµÃ*dz£ÖÉÄÛ¡£

ͬʱ£¬¹a´oµÄ¿ª¢Õ߶ÔÓÚ PHP5.3 Ìa¹(C)µÄ°uÀ¨±Õ°uÔÚÄÚµÄÐÂÌØÐÔ»¹ÔÚ³Ö¹ÛÍu̬¶È¡£PHP µÄ±Õ°uÌØÐÔÄ¿Ç°»¹ÊÇ´aeÔÚÓÚʵÑeÊÒÖУ¬ÆaÓ¦ÓÃÓÚʵ¼Ê¿ª¢ÈçҪͻÆƵIJ»½o½oÊÇÓiÑÔÌØÐÔ £¬»¹Òª¾­¹ýЧÂÊ¡¢°²È«ÐԵȽÃaeµÄ¿¼Ñe¡£
µ«ÏaÐÅ£¬ÈçÔ­ÎÄ×÷ÕßËuÑÔ£¬Ëae×Å PHP °ae±¾µÄÍƽø£¬PHP µÄ±Õ°uÓ¦Óó¡ºÏ»aÔ½À´Ô½Æµ
±¡£Ïñ µ±Äe PHP4 ת»»µ½ PHP5 Ò»Ñu£¬¶ÔÓiÑÔÐÂÌØÐÔµÄÊÊÓ¦£¬ÆaʵÊÇÖÖÍ´²¢¿iÀÖ×ŵĹý³Ì¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例