{"id":425,"date":"2025-06-10T20:15:53","date_gmt":"2025-06-10T12:15:53","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/425.html"},"modified":"2025-06-10T20:15:53","modified_gmt":"2025-06-10T12:15:53","slug":"php-%e5%ae%9a%e4%b9%89%e5%85%b7%e6%9c%89%e5%8f%af%e5%8f%98%e6%95%b0%e9%87%8f%e5%8f%82%e6%95%b0%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/425.html","title":{"rendered":"PHP: \u5b9a\u4e49\u5177\u6709\u53ef\u53d8\u6570\u91cf\u53c2\u6570\u7684\u51fd\u6570"},"content":{"rendered":"<p><body><\/p>\n<h2>&#27010;&#36848;<\/h2>\n<p>&#29702;&#35299;&#22914;&#20309;&#22312;PHP&#20013;&#31649;&#29702;&#20855;&#26377;&#21464;&#21442;&#25968;&#25968;&#37327;&#30340;&#20989;&#25968;&#65292;&#21487;&#20197;&#24110;&#21161;&#24320;&#21457;&#20154;&#21592;&#21019;&#24314;&#26356;&#28789;&#27963;&#21644;&#21160;&#24577;&#30340;&#20195;&#30721;&#32467;&#26500;&#12290;&#26412;&#25351;&#21335;&#20351;&#29992;&#20174;&#22522;&#30784;&#21040;&#39640;&#32423;&#30340;&#21508;&#31181;&#31034;&#20363;&#26469;&#25506;&#32034;&#27492;&#31867;&#20989;&#25968;&#30340;&#26680;&#24515;&#35201;&#32032;&#12290;<\/p>\n<h2>&#24320;&#22987;&#20351;&#29992;&#21487;&#21464;&#21442;&#25968;&#12290;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;&#20989;&#25968;&#20013;&#30340;&#21487;&#21464;&#25968;&#37327;&#30340;&#21442;&#25968;&#21487;&#20197;&#36890;&#36807;&#20351;&#29992;&#39764;&#26415;&#26041;&#27861;&#65288;magic methods&#65289;&#26469;&#22788;&#29702;&#12290;<code>func_get_args<\/code>&#20989;&#25968;&#65292;&#36820;&#22238;&#20256;&#20837;&#21442;&#25968;&#30340;&#25968;&#32452;&#12290;&#32771;&#34385;&#20197;&#19979;&#31034;&#20363;&#65306;<\/p>\n<pre><code>function sum() {\n    $numbers = func_get_args();\n    return array_sum($numbers);\n}\necho sum(1, 2); \/\/ 3\necho sum(1, 2, 3, 4); \/\/ 10\n<\/code><\/pre>\n<h3>&#20351;&#29992;&#21464;&#38271;&#21442;&#25968;&#21015;&#34920;&#65288;variadic functions&#65289;<\/h3>\n<p>PHP 5.6 &#24341;&#20837;&#20102;&#26356;&#20248;&#38597;&#30340;&#26041;&#24335;&#26469;&#22788;&#29702;&#21487;&#21464;&#25968;&#37327;&#30340;&#21442;&#25968;&#65306;&#20351;&#29992; &#8216;&#8230;&#8217; &#36816;&#31639;&#31526;&#12290;<\/p>\n<pre><code>function sum(...$numbers) {\n    return array_sum($numbers);\n}\necho sum(1, 2, 3); \/\/ 6\n<\/code><\/pre>\n<h3>&#32467;&#21512;&#22266;&#23450;&#21442;&#25968;&#21644;&#21464;&#38271;&#21442;&#25968;&#12290;<\/h3>\n<p>&#20320;&#21487;&#20197;&#29992;&#22266;&#23450;&#21644;&#21464;&#38271;&#21442;&#25968;&#26469;&#23450;&#20041;&#20989;&#25968;&#12290;&#22266;&#23450;&#21442;&#25968;&#20808;&#23450;&#20041;&#65292;&#28982;&#21518;&#26159;&#21464;&#38271;&#37096;&#20998;&#65306;<\/p>\n<pre><code>function addPrefix($prefix, ...$numbers) {\n    $result = [];\n    foreach ($numbers as $number) {\n        $result[] = $prefix . $number;\n    }\n    return $result;\n}\nprint_r(addPrefix('#', 1, 2, 3)); \/\/ Array ( [0] =&gt; #1 [1] =&gt; #2 [2] =&gt; #3 )\n<\/code><\/pre>\n<h3>&#31867;&#22411;&#25552;&#31034;&#21487;&#21464;&#21442;&#25968;<\/h3>\n<p>&#22312;PHP&#20013;&#65292;&#31867;&#22411;&#22768;&#26126;&#29992;&#20110;&#25351;&#23450;&#20256;&#36882;&#21040;&#20989;&#25968;&#30340;&#21464;&#37327;&#30340;&#25968;&#25454;&#31867;&#22411;&#12290;&#36825;&#31181;&#29305;&#24615;&#25193;&#23637;&#21040;&#20102;&#21487;&#21464;&#21442;&#25968;&#65288;variadic arguments&#65289;&#19978;&#12290;<\/p>\n<pre><code>function sumInts(int ...$numbers): int {\n    return array_sum($numbers);\n}\necho sumInts(1, 2, 3); \/\/ 6\n<\/code><\/pre>\n<p>&#35831;&#27880;&#24847;&#65292;&#20256;&#36882;&#32473;&#20989;&#25968;&#30340;&#27599;&#20010;&#20803;&#32032;&#29616;&#22312;&#37117;&#24517;&#39035;&#20026;&#25972;&#25968;&#65292;&#21542;&#21017;&#23558;&#25243;&#20986;&#19968;&#20010; TypeError &#24322;&#24120;&#12290;<\/p>\n<h2>&#39640;&#32423;&#29992;&#27861;<\/h2>\n<h3>&#36890;&#36807;&#32034;&#24341;&#26469;&#35775;&#38382;&#21442;&#25968;&#12290;<\/h3>\n<p>&#22914;&#26524;&#38656;&#35201;&#36890;&#36807;&#32034;&#24341;&#26469;&#35775;&#38382;&#29305;&#23450;&#30340;&#21442;&#25968;&#65292;&#20320;&#21487;&#20197;&#36825;&#26679;&#20570;&#65306;<\/p>\n<pre><code>function getArgument(...$args) {\n    if (func_num_args() &gt; 2) {\n        return $args[2];\n    }\n    return null;\n}\necho getArgument(1, 'a', true); \/\/ 1\n<\/code><\/pre>\n<h3>&#20256;&#36882;&#21464;&#37327;&#25968;&#37327;&#30340;&#21442;&#25968;&#21040;&#21478;&#19968;&#20010;&#20989;&#25968;<\/h3>\n<p>&#26377;&#26102;&#65292;&#20320;&#21487;&#33021;&#24076;&#26395;&#25910;&#38598;&#19981;&#23450;&#25968;&#37327;&#30340;&#21442;&#25968;&#24182;&#20256;&#36882;&#32473;&#21478;&#19968;&#20010;&#20989;&#25968;&#12290;&#36825;&#21487;&#20197;&#36890;&#36807;&#20197;&#19979;&#26041;&#24335;&#23454;&#29616;&#65306;<\/p>\n<pre><code>function spreadArgs(...$args) {\n    return implode('-', $args);\n}\n\nfunction wrapper() {\n    return spreadArgs(...func_get_args());\n}\necho wrapper('PHP', 'Variable', 'Arguments'); \/\/ 'PHP-Variable-Arguments'\n<\/code><\/pre>\n<h3>&#20351;&#29992;&#21453;&#23556;API<\/h3>\n<p>&#21453;&#23556;API&#20801;&#35768;&#23545;&#20989;&#25968;&#36827;&#34892; introspection&#65292;&#25552;&#20379;&#39640;&#32423;&#26041;&#27861;&#26469;&#20998;&#26512;&#21644;&#25805;&#32437;&#20989;&#25968;&#21442;&#25968;&#65306;<\/p>\n<pre><code>function printArgs(...$args) {\n    foreach ($args as $index =&gt; $arg) {\n        echo 'Argument ' . $index . ': ' . $arg . \"n\";\n    }\n}\n\n$function = new ReflectionFunction('printArgs');\n$function-&gt;invokeArgs(['Hello', 'world', '!']);\n<\/code><\/pre>\n<h2>&#22788;&#29702;&#21487;&#36873;&#21442;&#25968;<\/h2>\n<p>&#24120;&#24120;&#19982;&#21464;&#38271;&#21442;&#25968;&#21015;&#34920;&#19968;&#36215;&#20351;&#29992;&#26102;&#65292;&#21487;&#20197;&#21033;&#29992;&#21487;&#36873;&#21442;&#25968;&#36827;&#19968;&#27493;&#22686;&#24378;&#20989;&#25968;&#30340;&#28789;&#27963;&#24615;&#65306;<\/p>\n<pre><code>function greeting($greet = 'Hello', ...$names) {\n    return $greet . ' ' . implode(', ', $names);\n}\n\necho greeting(null, 'Alice', 'Bob'); \/\/ Hello Alice, Bob\n<\/code><\/pre>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#22312;&#26412;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#25506;&#35752;&#20102;&#20351;&#29992;&#21464;&#37327;&#25968;&#37327;&#21442;&#25968;&#23450;&#20041;&#20989;&#25968;&#30340;PHP&#30340;&#24378;&#22823;&#20043;&#22788;&#12290;&#20174;&#22522;&#26412;&#30340;&#20351;&#29992;&#24320;&#22987;&#65292;&#25105;&#20204;&#36880;&#27493;&#28145;&#20837;&#21040;&#26356;&#22797;&#26434;&#30340;&#20027;&#39064;&#12290;<code>func_get_args()<\/code>&#20026;&#20102;&#20805;&#20998;&#21457;&#25381;&#22810;&#24577;&#24615;&#65288;variadicity&#65289;&#21644;&#26356;&#36827;&#19968;&#27493;&#30340;&#21151;&#33021;&#65292;PHP&#20989;&#25968;&#30340;&#21442;&#25968;&#22788;&#29702;&#33021;&#21147;&#21487;&#20197;&#24471;&#21040;&#26174;&#33879;&#25552;&#21319;&#12290;&#28789;&#27963;&#22788;&#29702;&#21442;&#25968;&#30340;&#33021;&#21147;&#26497;&#22823;&#22320;&#22686;&#24378;&#20102;PHP&#20989;&#25968;&#30340;&#22810;&#21151;&#33021;&#24615;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#27010;&#36848; &#29702;&#35299;&#22914;&#20309;&#22312;PHP&#20013;&#31649;&#29702;&#20855;&#26377;&#21464;&#21442;&#25968;&#25968;&#37327;&#30340;&#20989;&#038;#25..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-425","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=425"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=425"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}