{"id":47649,"date":"2024-12-02T10:06:29","date_gmt":"2024-12-02T02:06:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47649\/"},"modified":"2024-12-02T10:06:29","modified_gmt":"2024-12-02T02:06:29","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%bc%95%e7%94%a8%e8%87%aa%e5%ae%9a%e4%b9%89%e5%af%b9%e8%b1%a1%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47649\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u4f7f\u7528\u5f15\u7528\u53ef\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u4ece\u800c\u5b9e\u73b0\u6301\u4e45\u6027\u66f4\u6539\uff1a\u5728\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u63d0\u793a\u4e2d\u4f7f\u7528 &amp;\u7b26\u53f7\u8fdb\u884c\u5f15\u7528\u4f20\u9012\u3002\u5bf9\u8c61\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u540e\uff0c\u51fd\u6570\u53ef\u4ee5\u4fee\u6539\u5176\u5c5e\u6027\u3002\u4ec5\u5728\u51fd\u6570\u9700\u8981\u4fee\u6539\u5bf9\u8c61\u72b6\u6001\u65f6\u4f7f\u7528\u5f15\u7528\u4f20\u9012\uff0c\u5e76\u786e\u4fdd\u6587\u6863\u660e\u786e\u8bf4\u660e\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729928011671c9b4bbe111.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u5bf9\u8c61\u7684\u72b6\u6001\u3002\u8fd9\u5728\u9700\u8981\u5bf9\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5bf9\u8c61\u8fdb\u884c\u6301\u4e45\u6027\u66f4\u6539\u7684\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>\u8981\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\uff0c\u8bf7\u5728\u53c2\u6570\u7c7b\u578b\u63d0\u793a\u4e2d\u4f7f\u7528 <code>&amp;<\/code>\u7b26\u53f7\uff1a<\/p>\n<pre>function changeObjectState(MyClass &amp;$object) {\n    \/\/ ...\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a <code>User<\/code> \u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a <code>firstName<\/code> \u5c5e\u6027\u3002\u6211\u4eec\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570 <code>setAndprintFirstName<\/code>\uff0c\u5b83\u5c06\u7528\u6237\u7684 <code>firstName<\/code> \u5c5e\u6027\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u503c\u5e76\u6253\u5370\u8be5\u503c\uff1a<\/p>\n<pre>class User {\n    public $firstName;\n\n    public function __construct($firstName) {\n        $this-&gt;firstName = $firstName;\n    }\n}\n\nfunction setAndPrintFirstName(User &amp;$user, $firstName) {\n    $user-&gt;firstName = $firstName;\n    echo $firstName;\n}\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u7528\u6237\u5bf9\u8c61\n$user = new User('John');\n\n\/\/ \u4f7f\u7528\u5f15\u7528\u4f20\u9012\u5bf9\u8c61\u5230\u51fd\u6570\nsetAndPrintFirstName($user, 'Jane');\n\n\/\/ \u8f93\u51fa: Jane<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u5f15\u7528\u4f20\u9012\uff0c<code>setAndPrintFirstName<\/code> \u51fd\u6570\u80fd\u591f\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684 <code>User<\/code> \u5bf9\u8c61\u7684\u72b6\u6001\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u4ece\u51fd\u6570\u5185\u90e8\u6253\u5370 $firstName \u7684\u66f4\u6539\u540e\u7684\u503c\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u4ec5\u5728\u51fd\u6570\u9700\u8981\u4fee\u6539\u5bf9\u8c61\u7684\u72b6\u6001\u65f6\u624d\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u3002<\/li>\n<li>\u603b\u662f\u786e\u4fdd\u51fd\u6570\u6587\u6863\u6e05\u695a\u5730\u6307\u5b9a\u5b83\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u3002<\/li>\n<li>\u907f\u514d\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u4fee\u6539\u5176\u4ed6\u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5 \u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b \u5728 PHP \u4e2d\uff0c\u4f7f\u7528\u5f15\u7528\u53ef\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u81ea\u5b9a\u4e49\u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u4ece\u800c\u5b9e\u73b0\u6301\u4e45\u6027\u66f4\u6539\uff1a\u5728\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u63d0\u793a\u4e2d\u4f7f\u7528 &amp;\u7b26\u53f7\u8fdb\u884c\u5f15\u7528\u4f20\u9012\u3002\u5bf9\u8c61\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u540e\uff0c\u51fd\u6570\u53ef\u4ee5\u4fee\u6539\u5176\u5c5e\u6027\u3002\u4ec5\u5728\u51fd\u6570\u9700\u8981\u4fee\u6539\u5bf9\u8c61\u72b6\u6001\u65f6\u4f7f\u7528\u5f15\u7528\u4f20\u9012\uff0c\u5e76\u786e\u4fdd\u6587\u6863\u660e\u786e\u8bf4\u660e\u3002 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5 \u5728 PHP \u4e2d\uff0c\u4f7f\u7528\u5f15\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u5bf9\u8c61\u7684\u72b6\u6001\u3002\u8fd9\u5728\u9700\u8981\u5bf9\u51fd\u6570\u8c03\u7528\u4e2d\u7684\u5bf9\u8c61\u8fdb\u884c\u6301\u4e45\u6027\u66f4\u6539\u7684\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\u3002 \u8bed\u6cd5 \u8981\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5f15\u7528\u4f20\u9012\u81ea\u5b9a\u4e49\u5bf9\u8c61\u65b9\u6cd5\uff0c\u8bf7\u5728\u53c2\u6570\u7c7b\u578b\u63d0\u793a\u4e2d\u4f7f\u7528 &amp;\u7b26\u53f7\uff1a function changeObjectState(MyClass &amp;$object) { \/\/ &#8230; } \u5b9e\u6218\u6848\u4f8b \u8003\u8651\u4e00\u4e2a User \u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a firstName \u5c5e\u6027\u3002\u6211\u4eec\u5e0c\u671b\u7f16\u5199\u4e00\u4e2a\u51fd\u6570 setAndprintFirstName\uff0c\u5b83\u5c06\u7528\u6237\u7684 firstName \u5c5e\u6027\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u503c\u5e76\u6253\u5370\u8be5\u503c\uff1a class User { public $firstName; public function __construct($firstName) { $this-&gt;firstName = $firstName; } } function setAndPrintFirstName(User &amp;$user, $firstName) { $user-&gt;firstName = $firstName; echo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47649","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47649"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47649\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}