{"id":1506,"date":"2025-09-07T19:10:15","date_gmt":"2025-09-07T19:10:15","guid":{"rendered":"https:\/\/robinluo.top\/?p=1506"},"modified":"2025-09-07T19:10:15","modified_gmt":"2025-09-07T19:10:15","slug":"php-%e9%97%ad%e5%8c%85%e4%bc%a0%e5%80%bc%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=1506","title":{"rendered":"php \u95ed\u5305\u4f20\u503c\u5904\u7406"},"content":{"rendered":"\n<h3>\u65b9\u6cd5 1\uff1a\u4f7f\u7528&nbsp;<code>use<\/code>&nbsp;\u5173\u952e\u5b57\u6309\u5f15\u7528\u4f20\u9012\uff08\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff09<\/h3>\n\n\n\n<p>\u8fd9\u662f<strong>\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5<\/strong>\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u4f7f\u7528\u00a0<code>use<\/code>\u00a0\u65f6\u5728\u53d8\u91cf\u524d\u52a0\u4e0a\u00a0<code>&amp;<\/code>\u00a0\u7b26\u53f7\uff0c\u4f60\u53ef\u4ee5<strong>\u6309\u5f15\u7528<\/strong>\u4f20\u9012\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u95ed\u5305\u5185\u5916\u7684\u53d8\u91cf\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u5728\u95ed\u5305\u5185\u4fee\u6539\u5b83\uff0c\u5916\u90e8\u7684\u503c\u4e5f\u4f1a\u540c\u6b65\u6539\u53d8\u3002php\u590d\u5236\u4e0b\u8f7d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$externalValue = 10;\r\n$anotherVariable = \"Hello\";\r\n\r\n$myClosure = function() use (&amp;$externalValue, $anotherVariable) {\r\n    \/\/ \u4fee\u6539\u6309\u5f15\u7528\u4f20\u9012\u7684\u53d8\u91cf\r\n    $externalValue += 50; \/\/ \u8fd9\u4f1a\u5f71\u54cd\u5916\u90e8\u7684 $externalValue\r\n    \r\n    \/\/ \u8fd9\u662f\u6309\u503c\u4f20\u9012\u7684\uff0c\u4fee\u6539\u5b83\u4e0d\u4f1a\u5f71\u54cd\u5916\u90e8\u7684 $anotherVariable\r\n    $anotherVariable .= \" World\"; \r\n    \r\n    echo \"Inside closure: externalValue = $externalValue, anotherVariable = $anotherVariable\\n\";\r\n};\r\n\r\n$myClosure(); \/\/ \u8f93\u51fa\uff1aInside closure: externalValue = 60, anotherVariable = Hello World\r\n\r\necho \"Outside: externalValue = $externalValue\\n\"; \/\/ \u8f93\u51fa\uff1aOutside: externalValue = 60\r\necho \"Outside: anotherVariable = $anotherVariable\\n\"; \/\/ \u8f93\u51fa\uff1aOutside: anotherVariable = Hello<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p><strong>\u5173\u952e\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>use (&amp;$variable)<\/code>\uff1a\u6309\u5f15\u7528\u4f20\u9012\uff0c\u5185\u90e8\u4fee\u6539\u5f71\u54cd\u5916\u90e8\u3002<\/li><li><code>use ($variable)<\/code>\uff1a\u6309\u503c\u4f20\u9012\uff08\u9ed8\u8ba4\uff09\uff0c\u5185\u90e8\u4fee\u6539<strong>\u4e0d<\/strong>\u5f71\u54cd\u5916\u90e8\u3002\u95ed\u5305\u4f7f\u7528\u7684\u662f\u5b83\u521b\u5efa\u65f6\u8be5\u53d8\u91cf\u7684\u4e00\u4e2a<strong>\u526f\u672c<\/strong>\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5 1\uff1a\u4f7f\u7528&nbsp;use&nbsp;\u5173\u952e\u5b57\u6309\u5f15\u7528\u4f20\u9012\uff08\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff09 \u8fd9\u662f\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u4f7f\u7528\u00a0use\u00a0\u65f6\u5728\u53d8\u91cf\u524d\u52a0\u4e0a\u00a0&amp;\u00a0\u7b26\u53f7\uff0c\u4f60\u53ef\u4ee5\u6309\u5f15\u7528\u4f20\u9012\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u95ed\u5305\u5185\u5916\u7684\u53d8\u91cf\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740\uff0c\u5728\u95ed\u5305\u5185\u4fee\u6539\u5b83\uff0c\u5916\u90e8\u7684\u503c\u4e5f\u4f1a\u540c\u6b65\u6539\u53d8\u3002php\u590d\u5236\u4e0b\u8f7d \u5173\u952e\u70b9\uff1a use (&amp;$variable)\uff1a\u6309\u5f15\u7528\u4f20\u9012\uff0c\u5185\u90e8\u4fee\u6539\u5f71\u54cd\u5916\u90e8\u3002 use ($variable)\uff1a\u6309\u503c\u4f20\u9012\uff08\u9ed8\u8ba4\uff09\uff0c\u5185\u90e8\u4fee\u6539\u4e0d\u5f71\u54cd\u5916\u90e8\u3002\u95ed\u5305\u4f7f\u7528\u7684\u662f\u5b83\u521b\u5efa\u65f6\u8be5\u53d8\u91cf\u7684\u4e00\u4e2a\u526f\u672c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[236],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1506"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1506"}],"version-history":[{"count":1,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1506\/revisions"}],"predecessor-version":[{"id":1507,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/1506\/revisions\/1507"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}