\u4e09\u3001\u603b\u7ed3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n\u4e00\u3001\u524d\u8a00<\/h3>\n
PHP 8.1 \u53d1\u5e03\u4e8e2021 \u5e74 11 \u6708 25 \u65e5\uff0c\u6700\u8fd1\u4e00\u6b21\u66f4\u65b0\u57282022 \u5e74 9 \u6708 29 \u65e5\uff0c\u6211\u4eec\u8fd9\u6b21\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u4e2a\u7248\u672c\u7684\u65b0\u7279\u6027\u3002<\/p>\n
\u5982\u679c\u4f60\u7684\u9879\u76ee\u6b63\u5728\u4f7f\u7528 8.1 \u7248\u672c\uff0c\u8bf7\u4f60\u8981\u6ce8\u610f\u8fd9\u4e9b\u5e76\u4e14\u52a0\u4ee5\u8fd0\u7528\u3002<\/p>\n
<\/p>\n
\u4e8c\u3001PHP 8.1 \u65b0\u7279\u6027<\/h3>\n\u7eaf\u4ea4\u53c9\u7c7b\u578b Pure Intersection Types<\/h4>\n
PHP 8.1 \u589e\u52a0\u4e86\u5bf9\u4ea4\u53c9\u7c7b\u578b\u7684\u652f\u6301\u3002 \u5b83\u7c7b\u4f3c\u4e8e PHP 8.0 \u4e2d\u5f15\u5165\u7684\u8054\u5408\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u7684\u7528\u9014\u6070\u6070\u76f8\u53cd\u3002<\/p>\n
\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5b83\u7684\u7528\u6cd5\uff0c\u6211\u4eec\u770b\u4e0b PHP \u4e2d\u7c7b\u578b\u58f0\u660e\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n
\u4f60\u53ef\u4ee5\u5c06\u7c7b\u578b\u58f0\u660e\u6dfb\u52a0\u5230\u51fd\u6570\u53c2\u6570\u3001\u8fd4\u56de\u503c\u548c\u7c7b\u5c5e\u6027\u3002 \u6b64\u5206\u914d\u79f0\u4e3a\u7c7b\u578b\u63d0\u793a\uff0c\u5e76\u786e\u4fdd\u8be5\u503c\u5728\u8c03\u7528\u65f6\u5177\u6709\u6b63\u786e\u7684\u7c7b\u578b\u3002 \u5426\u5219\uff0c\u5b83\u4f1a\u7acb\u5373\u629b\u51fa TypeError\u3002<\/p>\n
\u4f46\u662f\uff0c\u58f0\u660e\u5355\u4e00\u7c7b\u578b\u6709\u5c40\u9650\u6027\u3002 \u8054\u5408\u7c7b\u578b\u5141\u8bb8\u58f0\u660e\u5177\u6709\u591a\u79cd\u7c7b\u578b\u7684\u503c\uff0c\u5e76\u4e14\u8f93\u5165\u5fc5\u987b\u6ee1\u8db3\u81f3\u5c11\u4e00\u79cd\u58f0\u660e\u7684\u7c7b\u578b\u3002<\/p>\n
\u8bf7\u6ce8\u610f\u4f7f\u7528 & (AND) \u8fd0\u7b97\u7b26\u6765\u58f0\u660e\u4ea4\u53c9\u7c7b\u578b\u3002 \u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6211\u4eec\u4f7f\u7528 | (OR) \u8fd0\u7b97\u7b26\u6765\u58f0\u660e\u8054\u5408\u7c7b\u578b\u3002<\/p>\n
\u5728\u4ea4\u96c6\u7c7b\u578b\u4e2d\u4f7f\u7528\u5927\u591a\u6570\u6807\u51c6\u7c7b\u578b\u5c06\u5bfc\u81f4\u6c38\u8fdc\u65e0\u6cd5\u5b9e\u73b0\u7684\u7c7b\u578b\uff08\u4f8b\u5982\u6574\u6570\u548c\u5b57\u7b26\u4e32\uff09\u3002 \u56e0\u6b64\uff0c\u4ea4\u53c9\u7c7b\u578b\u53ea\u80fd\u5305\u62ec\u7c7b\u7c7b\u578b\uff08\u5373\u63a5\u53e3\u548c\u7c7b\u540d\uff09\u3002<\/p>\n
\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528\u4ea4\u53c9\u7c7b\u578b\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n
class<\/span> PureIntersectionTypesClass<\/span> { \n <\/span>\n\n private<\/span> Traversable&<\/span>Countable<\/span> $countableIterator<\/span>;<\/span>\n \n public<\/span> function<\/span> setIterator<\/span>(<\/span>Traversable&<\/span>Countable<\/span> $countableIterator<\/span>)<\/span>:<\/span> void<\/span> { \n <\/span>\n $this<\/span>-><\/span>countableIterator<\/span> =<\/span> $countableIterator<\/span>;<\/span>\n }<\/span>\n \n public<\/span> function<\/span> getIterator<\/span>(<\/span>)<\/span>:<\/span> Traversable<\/span>&<\/span>Countable { \n <\/span>\n return<\/span> $this<\/span>-><\/span>countableIterator<\/span>;<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\n\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf countableIterator \u4f5c\u4e3a\u4e24\u79cd\u7c7b\u578b\u7684\u4ea4\u96c6\uff1aTraversable \u548c Countable\u3002 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u58f0\u660e\u7684\u4e24\u79cd\u7c7b\u578b\u90fd\u662f\u63a5\u53e3\u3002<\/p>\n
\u4ea4\u53c9\u7c7b\u578b\u4e5f\u7b26\u5408\u5df2\u7ecf\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\u548c\u7ee7\u627f\u7684\u6807\u51c6 PHP \u53d8\u5316\u89c4\u5219\u3002 \u4f46\u662f\u8fd8\u6709\u4e24\u4e2a\u989d\u5916\u7684\u89c4\u5219\u6765\u8bf4\u660e\u4ea4\u53c9\u7c7b\u578b\u5982\u4f55\u4e0e\u5b50\u7c7b\u578b\u4ea4\u4e92\u3002<\/p>\n
PHP 8.1 \u4e0d\u80fd\u5728\u540c\u4e00\u4e2a\u58f0\u660e\u4e2d\u7ec4\u5408\u8054\u5408\u7c7b\u578b\u548c\u4ea4\u53c9\u7c7b\u578b\u3002 \u56e0\u6b64\uff0c\u5b83\u7684\u5b9e\u73b0\u88ab\u79f0\u4e3a\u201c\u7eaf\u201d\u4ea4\u53c9\u7c7b\u578b\u3002<\/p>\n
\u679a\u4e3e Enums<\/h4>\n
\u7f16\u7a0b\u8bed\u8a00\u4e2d\u6700\u5e38\u89c1\u7684\u679a\u4e3e\u793a\u4f8b\u662f\u5e03\u5c14\u7c7b\u578b\uff0c\u5176\u4e2d true \u548c false \u4f5c\u4e3a\u4e24\u80fd\u7684\u503c\u3002<\/p>\n
enum<\/span> Suit<\/span> { \n <\/span>\n case<\/span> Hearts<\/span>;<\/span>\n case<\/span> Diamonds<\/span>;<\/span>\n case<\/span> Clubs<\/span>;<\/span>\n case<\/span> Spades<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n\u5728\u8fd9\u91cc\uff0cSuit \u679a\u4e3e\u5b9a\u4e49\u4e86\u56db\u4e2a\u53ef\u80fd\u7684\u503c\uff1aHearts\u3001Diamonds\u3001Clubs \u548c Spades\u3002 \u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u503c\uff1aSuit::Hearts\u3001Suit::Diamonds\u3001Suit::Clubs \u548c Suit::Spades\u3002<\/p>\n
\u679a\u4e3e\u662f\u5728\u7c7b\u548c\u5bf9\u8c61\u4e4b\u4e0a\u6784\u5efa\u7684\u3002 \u679a\u4e3e\u4e0e\u7c7b\u3001\u63a5\u53e3\u548c\u7279\u5f81\u5171\u4eab\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n
\u4e0a\u9762\u63d0\u5230\u7684\u679a\u4e3e\u79f0\u4e3a\u7eaf\u679a\u4e3e\u3002<\/p>\n
\u5982\u679c\u60a8\u60f3\u4e3a\u4efb\u4f55\u60c5\u51b5\u63d0\u4f9b\u6807\u91cf\u7b49\u6548\u503c\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u652f\u6301\u7684\u679a\u4e3e\u3002 \u4f46\u662f\uff0c\u652f\u6301\u7684\u679a\u4e3e\u53ea\u80fd\u5177\u6709\u4e00\u79cd\u7c7b\u578b\uff0c\u5373 int \u6216 string\uff08\u4ece\u4e0d\u540c\u65f6\u5177\u6709\u4e24\u79cd\u7c7b\u578b\uff09\u3002<\/p>\n
enum<\/span> Suit<\/span>:<\/span> string<\/span> { \n <\/span>\n case<\/span> Hearts<\/span> =<\/span> 'H'<\/span>;<\/span>\n case<\/span> Diamonds<\/span> =<\/span> 'D'<\/span>;<\/span>\n case<\/span> Clubs<\/span> =<\/span> 'C'<\/span>;<\/span>\n case<\/span> Spades<\/span> =<\/span> 'S'<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\n\u6ce8\u610f\uff0c\u652f\u6301\u679a\u4e3e\u7684\u6240\u6709\u4e0d\u540c\u60c5\u51b5\u90fd\u5fc5\u987b\u5177\u6709\u552f\u4e00\u503c\u3002<\/p>\n
never<\/code> \u8fd4\u56de\u7c7b\u578b<\/h4>\nPHP 8.1 \u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a never \u7684\u65b0\u8fd4\u56de\u7c7b\u578b\u63d0\u793a\u3002 \u5728\u603b\u662f\u629b\u51fa\u6216\u9000\u51fa\u7684\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n
function<\/span> redirect<\/span>(<\/span>string<\/span> $uri<\/span>)<\/span>:<\/span> never<\/span> { \n <\/span>\n header<\/span>(<\/span>'Location: '<\/span> .<\/span> $uri<\/span>)<\/span>;<\/span>\n exit<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n \nfunction<\/span> redirectToLoginPage<\/span>(<\/span>)<\/span>:<\/span> never<\/span> { \n <\/span>\n redirect<\/span>(<\/span>'\/login'<\/span>)<\/span>;<\/span>\n}<\/span>\n<\/code><\/pre>\nnever<\/code> \u8fd4\u56de\u7c7b\u578b\u7684\u51fd\u6570\u5e94\u6ee1\u8db3\u4e09\u4e2a\u6761\u4ef6\uff1a<\/p>\n\n- \u4e0d\u5e94\u8be5\u660e\u786e\u5b9a\u4e49 return \u8bed\u53e5\u3002<\/li>\n
- \u4e0d\u5e94\u8be5\u9690\u5f0f\u5b9a\u4e49 return \u8bed\u53e5\uff08\u4f8b\u5982 if-else \u8bed\u53e5\uff09\u3002<\/li>\n
- \u5fc5\u987b\u4ee5\u9000\u51fa\u8bed\u53e5\uff08\u663e\u5f0f\u6216\u9690\u5f0f\uff09\u7ed3\u675f\u5176\u6267\u884c\u3002<\/li>\n<\/ul>\n
never<\/code>\u8fd4\u56de\u7c7b\u578b\u4e0e void<\/code> \u8fd4\u56de\u7c7b\u578b\u6709\u8bb8\u591a\u76f8\u4f3c\u4e4b\u5904\u3002 \u5b83\u4eec\u90fd\u786e\u4fdd\u51fd\u6570\u6216\u65b9\u6cd5\u4e0d\u8fd4\u56de\u503c\u3002 \u4f46\u662f\uff0c\u5b83\u901a\u8fc7\u6267\u884c\u66f4\u4e25\u683c\u7684\u89c4\u5219\u800c\u6709\u6240\u4e0d\u540c\u3002 \u4f8b\u5982\uff0c\u4e00\u4e2a void \u58f0\u660e\u7684\u51fd\u6570\u4ecd\u7136\u53ef\u4ee5\u5728\u6ca1\u6709\u663e\u5f0f\u503c\u7684\u60c5\u51b5\u4e0b\u8fd4\u56de\uff0c\u4f46\u4f60\u4e0d\u80fd\u5bf9\u4e00\u4e2anever<\/code>\u8fd4\u56de\u7c7b\u578b\u7684\u51fd\u6570\u505a\u540c\u6837\u7684\u4e8b\u60c5\u3002<\/p>\nFibers \u7ea4\u7a0b<\/h4>\n
\u4ece\u5386\u53f2\u4e0a\u770b\uff0cPHP \u4ee3\u7801\u51e0\u4e4e\u603b\u662f\u540c\u6b65\u4ee3\u7801\u3002 \u4ee3\u7801\u6267\u884c\u4f1a\u6682\u505c\uff0c\u76f4\u5230\u8fd4\u56de\u7ed3\u679c\uff0c\u5373\u4f7f\u5bf9\u4e8e I\/O \u64cd\u4f5c\u4e5f\u662f\u5982\u6b64\u3002 \u60a8\u53ef\u4ee5\u60f3\u8c61\u4e3a\u4ec0\u4e48\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u4f7f\u4ee3\u7801\u6267\u884c\u901f\u5ea6\u53d8\u6162\u3002<\/p>\n
Fiber \u662f PHP \u901a\u8fc7\u865a\u62df\u7ebf\u7a0b\uff08\u6216\u7eff\u8272\u7ebf\u7a0b\uff09\u5904\u7406\u5e76\u884c\u6027\u7684\u65b9\u5f0f\u3002 \u5b83\u8bd5\u56fe\u901a\u8fc7\u5141\u8bb8 PHP \u51fd\u6570\u4e2d\u65ad\u800c\u4e0d\u5f71\u54cd\u6574\u4e2a\u8c03\u7528\u5806\u6808\u6765\u6d88\u9664\u540c\u6b65\u548c\u5f02\u6b65\u4ee3\u7801\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n
\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Fibers \u5f00\u53d1\u5168\u6808\u3001\u53ef\u4e2d\u65ad\u7684 PHP \u51fd\u6570\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u5728 PHP \u4e2d\u5b9e\u73b0\u534f\u4f5c\u5f0f\u591a\u4efb\u52a1\u5904\u7406\u3002<\/p>\n
<\/p>\n
$fiber<\/span> =<\/span> new<\/span> Fiber<\/span>(<\/span>function<\/span> (<\/span>)<\/span>:<\/span> void<\/span> { \n <\/span>\n $value<\/span> =<\/span> Fiber<\/span>::<\/span>suspend<\/span>(<\/span>'fiber'<\/span>)<\/span>;<\/span>\n echo<\/span> \"Value used to resume fiber: \"<\/span>,<\/span> $value<\/span>,<\/span> \"\\n\"<\/span>;<\/span>\n}<\/span>)<\/span>;<\/span>\n \n$value<\/span> =<\/span> $fiber<\/span>-><\/span>start<\/span>(<\/span>)<\/span>;<\/span> \/\/\u542f\u52a8<\/span>\n \necho<\/span> \"Value from fiber suspending: \"<\/span>,<\/span> $value<\/span>,<\/span> \"\\n\"<\/span>;<\/span>\n \n$fiber<\/span>-><\/span>resume<\/span>(<\/span>'test'<\/span>)<\/span>;<\/span> \/\/\u6062\u590d<\/span>\n<\/code><\/pre>\n\u4e0a\u9762\u7684\u793a\u4f8b\u8f93\u51fa\u5982\u4e0b\u3002<\/p>\n
Value from fiber suspending:<\/span> fiber\nValue used to resume fiber:<\/span> test\n<\/code><\/pre>\nreadonly \u53ea\u8bfb\u5c5e\u6027<\/h4>\n
PHP 8.1 \u589e\u52a0\u4e86\u5bf9\u53ea\u8bfb\u5c5e\u6027\u7684\u652f\u6301\u3002 \u5b83\u4eec\u53ea\u80fd\u4ece\u58f0\u660e\u5b83\u4eec\u7684\u8303\u56f4\u521d\u59cb\u5316\u4e00\u6b21\u3002 \u521d\u59cb\u5316\u540e\uff0c\u60a8\u5c06\u65e0\u6cd5\u4fee\u6539\u5b83\u4eec\u7684\u503c\u3002 \u8fd9\u6837\u505a\u4f1a\u5f15\u53d1\u9519\u8bef\u5f02\u5e38\u3002<\/p>\n
class<\/span> Test<\/span> { \n <\/span>\n public<\/span> readonly string<\/span> $kinsta<\/span>;<\/span>\n \n public<\/span> function<\/span> __construct<\/span>(<\/span>string<\/span> $kinsta<\/span>)<\/span> { \n <\/span>\n \/\/ \u521d\u59cb\u5316<\/span>\n $this<\/span>-><\/span>kinsta<\/span> =<\/span> $kinsta<\/span>;<\/span>\n }<\/span>\n}<\/span>\n<\/code><\/pre>\nreadonly \u5c5e\u6027\u5728\u7c7b\u5185\u90e8\u548c\u5916\u90e8\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4e0d\u53d8\u6027\u4fdd\u8bc1\u3002<\/p>\n
\u4e09\u3001\u603b\u7ed3<\/h3>\n
PHP 8.1 \u7684\u65b0\u7279\u6027\u5f88\u591a\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u91cd\u70b9\u653e\u5728<\/p>\n
\n- \u679a\u4e3e<\/li>\n
- never\u7c7b\u578b<\/li>\n
- \u4ea4\u53c9\u7c7b\u578b<\/li>\n
- \u53ea\u8bfb\u5c5e\u6027<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u3010PHP\u3011PHP 8.1 \u7684\u65b0\u7279\u6027\u4e00\u89c8 | \u4ea4\u53c9\u7c7b\u578b\u3001\u679a\u4e3e\u3001never \u8fd4\u56de\u7c7b\u578b\u3001Fibers \u7ea4\u7a0b\u3001readonly \u53ea\u8bfb\u5c5e\u6027\u5728\u5de5\u4f5c\u8868\u4e2d\u4ee5\u5217\u6216\u884c\u7684\u5f62\u5f0f...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8128"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8128"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8128\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}