{"id":49276,"date":"2024-12-02T11:37:45","date_gmt":"2024-12-02T03:37:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49276\/"},"modified":"2024-12-02T11:37:45","modified_gmt":"2024-12-02T03:37:45","slug":"php-%e7%9a%84%e6%96%b0%e5%8a%9f%e8%83%bd-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49276\/","title":{"rendered":"PHP \u7684\u65b0\u529f\u80fd"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u7684\u65b0\u529f\u80fd<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aPHP \u7684\u65b0\u529f\u80fd\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/17323709866741e22abaee8.jpg\" class=\"aligncenter\" title=\"PHP \u7684\u65b0\u529f\u80fd\u63d2\u56fe\" alt=\"PHP \u7684\u65b0\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<h2> php 8.4\uff1a\u65b0\u589e\u529f\u80fd\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83 <\/h2>\n<p>php 8.4 \u73b0\u5df2\u63a8\u51fa\uff0c\u5e26\u6765\u4e86\u4e00\u4e9b\u4ee4\u4eba\u5174\u594b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7b80\u5316\u7f16\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\u3002\u672c\u6587\u901a\u8fc7\u7b80\u5355\u7684\u793a\u4f8b\u89e3\u91ca\u4e86\u6700\u91cd\u8981\u7684\u66f4\u65b0\uff0c\u4f7f\u5404\u79cd\u6280\u80fd\u6c34\u5e73\u7684\u5f00\u53d1\u4eba\u5458\u90fd\u80fd\u8f7b\u677e\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u3002<\/p>\n<hr>\n<h2> 1. \u5c5e\u6027\u6302\u94a9 <\/h2>\n<p>\u5c5e\u6027\u6302\u94a9\u53ef\u8ba9\u60a8\u81ea\u5b9a\u4e49\u83b7\u53d6\u6216\u8bbe\u7f6e\u5c5e\u6027\u65f6\u53d1\u751f\u7684\u60c5\u51b5\u3002\u8fd9\u6d88\u9664\u4e86\u5bf9\u5355\u72ec\u7684 getter \u548c setter \u65b9\u6cd5\u7684\u9700\u8981\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class user {\n    private string $firstname;\n    private string $lastname;\n\n    public function __construct(string $firstname, string $lastname) {\n        $this-&gt;firstname = $firstname;\n        $this-&gt;lastname = $lastname;\n    }\n\n    \/\/ this property combines first and last name\n    public string $fullname {\n        get =&gt; $this-&gt;firstname . ' ' . $this-&gt;lastname;\n        set =&gt; [$this-&gt;firstname, $this-&gt;lastname] = explode(' ', $value, 2);\n    }\n}\n\n$user = new user('john', 'doe');\necho $user-&gt;fullname; \/\/ output: john doe\n\n$user-&gt;fullname = 'jane smith'; \/\/ updates first and last names\necho $user-&gt;fullname; \/\/ output: jane smith\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u7528\uff1a<\/strong><\/p>\n<p> \u5c5e\u6027\u6302\u94a9\u4f7f\u60a8\u7684\u4ee3\u7801\u66f4\u5e72\u51c0\u5e76\u51cf\u5c11\u6837\u677f\u6587\u4ef6\u3002<\/p>\n<hr>\n<h2> 2. \u53ef\u89c1\u6027\u4e0d\u5bf9\u79f0 <\/h2>\n<p>\u60a8\u73b0\u5728\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7ea7\u522b\u7684\u53ef\u89c1\u6027\u6765\u8bfb\u53d6\u548c\u5199\u5165\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5c5e\u6027\u53ef\u4ee5\u88ab\u6240\u6709\u4eba\u8bfb\u53d6\uff0c\u4f46\u53ea\u80fd\u88ab\u7c7b\u672c\u8eab\u5199\u5165\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class bankaccount {\n    public private(set) float $balance; \/\/ public read, private write\n\n    public function __construct(float $initialbalance) {\n        $this-&gt;balance = $initialbalance; \/\/ allowed here\n    }\n\n    public function deposit(float $amount): void {\n        $this-&gt;balance += $amount; \/\/ allowed here\n    }\n}\n\n$account = new bankaccount(100.0);\necho $account-&gt;balance; \/\/ output: 100\n\n$account-&gt;deposit(50.0); \/\/ adds 50 to the balance\necho $account-&gt;balance; \/\/ output: 150\n\n\/\/ the following line will cause an error:\n\/\/ $account-&gt;balance = 200.0;\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u7528\uff1a<\/strong><\/p>\n<p> \u6b64\u529f\u80fd\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u63a7\u5236\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u66f4\u65b0\u65b9\u5f0f\u3002<\/p>\n<hr>\n<h2> 3. \u65b0\u7684\u6570\u7ec4\u51fd\u6570 <\/h2>\n<p>php 8.4 \u6dfb\u52a0\u4e86\u65b0\u7684\u6570\u7ec4\u51fd\u6570\uff0c\u4f7f\u60a8\u65e0\u9700\u7f16\u5199\u624b\u52a8\u5faa\u73af\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>$numbers = [1, 2, 3, 4, 5];\n\n\/\/ find the first even number\n$firsteven = array_find($numbers, fn($n) =&gt; $n % 2 === 0);\necho $firsteven; \/\/ output: 2\n\n\/\/ check if any number is greater than 4\n$hasbignumber = array_any($numbers, fn($n) =&gt; $n &gt; 4);\nvar_dump($hasbignumber); \/\/ output: bool(true)\n\n\/\/ check if all numbers are positive\n$allpositive = array_all($numbers, fn($n) =&gt; $n &gt; 0);\nvar_dump($allpositive); \/\/ output: bool(true)\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u7528\uff1a<\/strong><\/p>\n<p> \u8fd9\u4e9b\u51fd\u6570\u4f7f\u6570\u7ec4\u64cd\u4f5c\u7f16\u5199\u8d77\u6765\u66f4\u5feb\u3001\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n<hr>\n<h2> 4. \u7b80\u5316\u7684\u5bf9\u8c61\u5b9e\u4f8b\u5316 <\/h2>\n<p>\u60a8\u73b0\u5728\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5e76\u7acb\u5373\u8c03\u7528\u5b83\u7684\u65b9\u6cd5\uff0c\u800c\u65e0\u9700\u5c06\u5b9e\u4f8b\u5316\u653e\u5728\u62ec\u53f7\u4e2d\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class logger {\n    public function log(string $message): void {\n        echo $message;\n    }\n}\n\n\/\/ create an object and call a method in one step\nnew logger()-&gt;log('logging a message'); \/\/ output: logging a message\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u7528\uff1a<\/strong><\/p>\n<p> \u5b83\u51cf\u5c11\u4e86\u4e0d\u5fc5\u8981\u7684\u8bed\u6cd5\uff0c\u4f7f\u60a8\u7684\u4ee3\u7801\u66f4\u5e72\u51c0\u3002<\/p>\n<hr>\n<h2> 5. \u5f03\u7528\u9690\u5f0f\u53ef\u4e3a\u7a7a\u7c7b\u578b <\/h2>\n<p>php 8.4 \u8981\u6c42\u60a8\u663e\u5f0f\u58f0\u660e\u53c2\u6570\u4f55\u65f6\u53ef\u4ee5\u4e3a null\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u5bb9\u6613\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>\/\/ php 8.4 (recommended):\nfunction process(?string $data = null) {\n    echo $data ?? 'no data provided';\n}\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u7528\uff1a<\/strong><\/p>\n<p> \u663e\u5f0f\u58f0\u660e\u53ef\u4ee5\u9632\u6b62\u6df7\u6dc6\u5e76\u51cf\u5c11\u6f5c\u5728\u7684\u9519\u8bef\u3002<\/p>\n<hr>\n<h2> 6. \u60f0\u6027\u5bf9\u8c61 <\/h2>\n<p>\u60f0\u6027\u5bf9\u8c61\u8ba9\u60a8\u53ef\u4ee5\u5ef6\u8fdf\u521b\u5efa\u5bf9\u8c61\u76f4\u5230\u5b9e\u9645\u4f7f\u7528\u5b83\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u8d44\u6e90\u3002<\/p>\n<h3> \u4f8b\u5b50\uff1a <\/h3>\n<pre>class ExpensiveResource {\n    public function __construct() {\n        \/\/ Simulate a time-consuming setup\n        sleep(2);\n    }\n\n    public function doWork(): void {\n        echo 'Working...';\n    }\n}\n\n\/\/ Use a lazy object to delay creation\n$initializer = fn() =&gt; new ExpensiveResource();\n$reflector = new ReflectionClass(ExpensiveResource::class);\n$resource = $reflector-&gt;newLazyProxy($initializer);\n\n\/\/ The object isn't created yet\n$resource-&gt;doWork(); \/\/ Now the object is created and \"Working...\" is printed\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u6709\u7528\uff1a<\/strong><\/p>\n<p> \u8fd9\u5728\u5904\u7406\u6602\u8d35\u7684\u64cd\u4f5c\u6216\u5927\u578b\u7cfb\u7edf\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<hr>\n<h2> \u7ed3\u8bba <\/h2>\n<p>php 8.4 \u5f15\u5165\u4e86\u591a\u9879\u529f\u80fd\uff0c\u4f7f\u7f16\u7801\u66f4\u7b80\u5355\u3001\u66f4\u5f3a\u5927\uff1a<\/p>\n<ul>\n<li> <strong>property hooks<\/strong>\uff1a\u66ff\u6362 getter \u548c setter \u51fd\u6570\u3002<\/li>\n<li> <strong>\u4e0d\u5bf9\u79f0\u53ef\u89c1\u6027<\/strong>\uff1a\u66f4\u597d\u5730\u63a7\u5236\u8d22\u4ea7\u8bbf\u95ee\u3002<\/li>\n<li> <strong>\u65b0\u6570\u7ec4\u51fd\u6570<\/strong>\uff1a\u7b80\u5316\u5e38\u89c1\u6570\u7ec4\u64cd\u4f5c\u3002<\/li>\n<li> <strong>\u7b80\u5316\u7684\u5bf9\u8c61\u5b9e\u4f8b\u5316<\/strong>\uff1a\u66f4\u6e05\u6670\u7684\u5bf9\u8c61\u521b\u5efa\u3002<\/li>\n<li> <strong>\u5f03\u7528\u9690\u5f0f\u53ef\u7a7a\u7c7b\u578b<\/strong>\uff1a\u66f4\u5b89\u5168\u3001\u66f4\u6e05\u6670\u7684\u4ee3\u7801\u3002<\/li>\n<li> <strong>\u60f0\u6027\u5bf9\u8c61<\/strong>\uff1a\u901a\u8fc7\u5ef6\u8fdf\u5bf9\u8c61\u521b\u5efa\u6765\u8282\u7701\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<p>\u65e0\u8bba\u60a8\u662f\u521d\u5b66\u8005\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u4eba\u5458\uff0c\u8fd9\u4e9b\u66f4\u65b0\u90fd\u5c06\u4f7f php \u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u6109\u5feb\u3002\u4ece\u4eca\u5929\u5f00\u59cb\u63a2\u7d22 php 8.4\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u7684\u65b0\u529f\u80fd \u6536\u85cf \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aPHP \u7684\u65b0\u529f\u80fd\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ php 8.4\uff1a\u65b0\u589e\u529f\u80fd\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83 php 8.4 \u73b0\u5df2\u63a8\u51fa\uff0c\u5e26\u6765\u4e86\u4e00\u4e9b\u4ee4\u4eba\u5174\u594b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7b80\u5316\u7f16\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\u3002\u672c\u6587\u901a\u8fc7\u7b80\u5355\u7684\u793a\u4f8b\u89e3\u91ca\u4e86\u6700\u91cd\u8981\u7684\u66f4\u65b0\uff0c\u4f7f\u5404\u79cd\u6280\u80fd\u6c34\u5e73\u7684\u5f00\u53d1\u4eba\u5458\u90fd\u80fd\u8f7b\u677e\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u3002 1. \u5c5e\u6027\u6302\u94a9 \u5c5e\u6027\u6302\u94a9\u53ef\u8ba9\u60a8\u81ea\u5b9a\u4e49\u83b7\u53d6\u6216\u8bbe\u7f6e\u5c5e\u6027\u65f6\u53d1\u751f\u7684\u60c5\u51b5\u3002\u8fd9\u6d88\u9664\u4e86\u5bf9\u5355\u72ec\u7684 getter \u548c setter \u65b9\u6cd5\u7684\u9700\u8981\u3002 \u4f8b\u5b50\uff1a class user { private string $firstname; private string $lastname; public function __construct(string $firstname, string $lastname) { $this-&gt;firstname = $firstname; $this-&gt;lastname = $lastname; } \/\/ this property combines first and last name public string $fullname { get =&gt; [&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-49276","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49276","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=49276"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49276\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}