{"id":484,"date":"2025-06-10T21:08:21","date_gmt":"2025-06-10T13:08:21","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/484.html"},"modified":"2025-06-10T21:08:21","modified_gmt":"2025-06-10T13:08:21","slug":"php-%e4%b8%ad%e7%9a%84%e7%b1%bb%e5%b1%9e%e6%80%a7%e7%b1%bb%e5%9e%8b%e5%a3%b0%e6%98%8e%ef%bc%885-%e4%b8%aa%e7%a4%ba%e4%be%8b%ef%bc%89","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/484.html","title":{"rendered":"PHP \u4e2d\u7684\u7c7b\u5c5e\u6027\u7c7b\u578b\u58f0\u660e\uff085 \u4e2a\u793a\u4f8b\uff09"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#31867;&#22411;&#22768;&#26126;&#65292;&#20063;&#31216;&#20026;&#31867;&#22411;&#25552;&#31034;&#65292;&#20801;&#35768;&#24320;&#21457;&#32773;&#22312;&#20989;&#25968;&#22768;&#26126;&#12289;&#20989;&#25968;&#36820;&#22238;&#31867;&#22411;&#25110;&#33258;PHP 7.4&#29256;&#20197;&#26469;&#30340;&#31867;&#23646;&#24615;&#30340;&#25968;&#25454;&#31867;&#22411;&#20013;&#25351;&#23450;&#21442;&#25968;&#39044;&#26399;&#25968;&#25454;&#31867;&#22411;&#12290;&#27492;&#21151;&#33021;&#36890;&#36807;&#30830;&#20445;&#23646;&#24615;&#34987;&#27491;&#30830;&#20351;&#29992;&#26469;&#22686;&#24378;&#20195;&#30721;&#30340;&#20581;&#22766;&#24615;&#65292;&#20174;&#32780;&#25552;&#39640;&#38169;&#35823;&#22788;&#29702;&#33021;&#21147;&#24182;&#20419;&#36827;&#20195;&#30721;&#21487;&#35835;&#24615;&#21644;&#32500;&#25252;&#24615;&#12290;<\/p>\n<p>&#22312;&#26412;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#23558;&#36890;&#36807;&#20116;&#31181;&#36880;&#27493;&#36827;&#38454;&#30340;&#20363;&#23376;&#26469;&#25506;&#32034;&#22914;&#20309;&#20351;&#29992;&#31867;&#22411;&#22768;&#26126;&#20026;PHP&#31867;&#23646;&#24615;&#12290;&#20174;&#22522;&#30784;&#24320;&#22987;&#65292;&#25105;&#20204;&#23558;&#20250;&#36880;&#28176;&#21521;&#26356;&#22797;&#26434;&#30340;&#24212;&#29992;&#21457;&#23637;&#65292;&#28436;&#31034;&#31867;&#22411;&#22768;&#26126;&#23545;PHP&#24320;&#21457;&#30340;&#24433;&#21709;&#21644;&#28789;&#27963;&#24615;&#12290;<\/p>\n<h2>&#31034;&#20363; 1&#65306;&#22522;&#26412;&#31867;&#22411;&#22768;&#26126;<\/h2>\n<pre><code>class Employee {\n    public string $name;\n    public int $age;\n}\n\n$emp = new Employee();\n$emp-&gt;name = 'John Doe'; \/\/ Valid\necho $emp-&gt;name; \/\/ Outputs: John Doe\n$emp-&gt;age = 30; \/\/ Valid\necho $emp-&gt;age; \/\/ Outputs: 30\n<\/code><\/pre>\n<p>&#36825;&#20010;&#31034;&#20363;&#23637;&#31034;&#20102;&#31867;&#22411;&#22768;&#26126;&#22312;&#31867;&#23646;&#24615;&#20013;&#30340;&#31616;&#21333;&#20351;&#29992;&#12290;&#22312;&#36825;&#37324;&#65292;&#30340;<code>Employee<\/code>&#31867;&#26377;&#20004;&#20010;&#23646;&#24615;&#12290;<code>name<\/code>&#21644;&#25105;&#26377;&#20160;&#20040;&#20107;&#24773;&#21527;&#65311;<code>age<\/code>&#24050;&#32463;&#23459;&#24067;&#20102;&#12290;<code>string<\/code>and&#20063;&#26159;&#65292;&#25105;&#20204;&#32487;&#32493;&#21543;&#12290;<code>int<\/code>&#31867;&#22411;&#19981;&#21305;&#37197;&#26102;&#65292;&#20250;&#24341;&#21457;&#19968;&#20010; TypeError &#24322;&#24120;&#12290;<\/p>\n<h2>&#31034;&#20363; 2&#65306;&#21487;&#36873;&#31867;&#22411;<\/h2>\n<pre><code>class Person {\n    public ?string $name = null;\n    public ?int $age = null;\n}\n\n$person = new Person();\n$person-&gt;name = 'Alice'; \/\/ Also allows setting to null\n$person-&gt;age = null; \/\/ Valid\n<\/code><\/pre>\n<p>&#22312;&#36825;&#31181;&#22330;&#26223;&#19979;&#65292;&#31867;&#30340;&#23646;&#24615;&#36890;&#36807;&#22312;&#31867;&#22411;&#21069;&#38754;&#21152;&#19978;&#38382;&#21495;&#65288;?&#65289;&#26469;&#22768;&#26126;&#20026;&#21487;&#31354;&#30340;&#12290;&#36825;&#20801;&#35768;&#29305;&#23450;&#31867;&#22411;&#25110;&#38750;&#29305;&#23450;&#31867;&#22411;&#30340;&#20540;&#12290;<code>null<\/code>&#20998;&#37197;&#21040;&#29289;&#19994;&#65292;&#20197;&#20415;&#22312;&#25151;&#20135;&#23578;&#26410;&#26377;&#20215;&#20540;&#30340;&#24773;&#20917;&#19979;&#22788;&#29702;&#26696;&#20214;&#30340;&#28789;&#27963;&#24615;&#12290;<\/p>\n<h2>&#31034;&#20363; 3&#65306;&#32852;&#21512;&#31867;&#22411;&#65288;&#22312; PHP 8.0 &#20013;&#24341;&#20837;&#30340;&#65289;<\/h2>\n<pre><code>class Vehicle {\n    public int|float $speed;\n}\n\n$car = new Vehicle();\n$car-&gt;speed = 55; \/\/ Valid\n$car-&gt;speed = 70.5; \/\/ Also valid\n<\/code><\/pre>\n<p>PHP 8.0 &#24341;&#20837;&#20102;&#32852;&#21512;&#31867;&#22411;&#65292;&#20801;&#35768;&#19968;&#20010;&#23646;&#24615;&#22768;&#26126;&#20026;&#25509;&#21463;&#22810;&#31181;&#31867;&#22411;&#30340;&#31867;&#22411;&#12290;<code>Vehicle<\/code>&#31867;&#28436;&#31034;&#20102;&#20351;&#29992;&#32852;&#21512;&#31867;&#22411;&#30340;&#26041;&#27861;&#12290;<code>speed<\/code>&#23646;&#24615;&#12290;&#23427;&#21487;&#20197;&#25509;&#21463;&#25972;&#25968;&#21644;&#28014;&#28857;&#25968;&#65292;&#20197;&#28385;&#36275;&#19981;&#21516;&#30340;&#38656;&#27714;&#25552;&#20379;&#28789;&#27963;&#24615;&#12290;<\/p>\n<h2>&#31034;&#20363; 4&#65306;&#22797;&#26434;&#31867;&#22411;<\/h2>\n<pre><code>class BookList {\n    public array $books = [];\n}\n\n$library = new BookList();\n$library-&gt;books[] = '1984';\n$library-&gt;books[] = 'Brave New World';\n<\/code><\/pre>\n<p>&#25903;&#25345;&#22797;&#26434;&#25968;&#25454;&#31867;&#22411;&#65292;&#22914;&#25968;&#32452;&#12290;&#20363;&#22914;&#65292;&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#26377;&#65306;<code>BookList<\/code>&#31867;&#26377;&#19968;&#20010;&#12290;<code>books<\/code>&#24847;&#22270;&#25345;&#26377;&#20070;&#21517;&#25968;&#32452;&#30340;&#23646;&#24615;&#12290;&#27492;&#31034;&#20363;&#23637;&#31034;&#20102;&#31867;&#22411;&#22768;&#26126;&#22312;&#30830;&#20445;&#23646;&#24615;&#21253;&#21547;&#23450;&#20041;&#25968;&#25454;&#32467;&#26500;&#26041;&#38754;&#30340;&#24378;&#22823;&#21151;&#33021;&#65292;&#36825;&#26377;&#21161;&#20110;&#38450;&#27490;&#38169;&#35823;&#21644;&#19981;&#24403;&#20351;&#29992;&#12290;<\/p>\n<h2>&#31034;&#20363; 5&#65306;&#20351;&#29992;&#23646;&#24615;&#36827;&#34892;&#31867;&#22411;&#23646;&#24615;&#65288;PHP 8.1 &#21450;&#20197;&#19978;&#29256;&#26412;&#65289;<\/h2>\n<p>&#26412;&#20363;&#23637;&#31034;&#20102;PHP&#30340;&#23646;&#24615;&#29305;&#24449;&#21450;&#20854;&#31867;&#22411;&#23646;&#24615;&#30340;&#21151;&#33021;&#12290;&#36890;&#36807;&#20026;&#31867;&#23646;&#24615;&#24212;&#29992;&#19968;&#20010;&#29305;&#24615;&#65292;&#24320;&#21457;&#20154;&#21592;&#21487;&#20197;&#22312;&#23646;&#24615;&#22768;&#26126;&#20013;&#30452;&#25509;&#25351;&#23450;&#20803;&#25968;&#25454;&#65292;&#36825;&#20026;&#31867;&#23450;&#20041;&#22686;&#28155;&#20102;&#39069;&#22806;&#30340;&#31283;&#20581;&#24615;&#21644;&#28789;&#27963;&#24615;&#12290;<\/p>\n<pre><code>#[Attribute]\nclass ExampleAttribute {\n    public function __construct(public string $description) {}\n}\n\nclass AdvancedExample {\n    \/\/ Use the ExampleAttribute to add a description to the specialProperty\n    #[ExampleAttribute(description: \"This is a special string property.\")]\n    public string $specialProperty;\n\n    public function __construct(string $specialProperty) {\n        $this-&gt;specialProperty = $specialProperty;\n        $this-&gt;validateProperties();\n    }\n\n    \/\/ Method to validate property values based on attributes\n    private function validateProperties(): void {\n        $reflectionClass = new ReflectionClass($this);\n        foreach ($reflectionClass-&gt;getProperties() as $property) {\n            $attributes = $property-&gt;getAttributes(ExampleAttribute::class);\n            foreach ($attributes as $attribute) {\n                $attributeInstance = $attribute-&gt;newInstance();\n                \/\/ Simple validation: check if the property is non-empty based on its description\n                if (empty($this-&gt;{$property-&gt;getName()})) {\n                    throw new InvalidArgumentException(\"The property '{$property-&gt;getName()}' described as '{$attributeInstance-&gt;description}' cannot be empty.\");\n                }\n                \/\/ Additional validation logic could be added here\n                echo \"Property '{$property-&gt;getName()}' is valid with description: '{$attributeInstance-&gt;description}'.n\";\n            }\n        }\n    }\n}\n\n\/\/ Example usage:\ntry {\n    $example = new AdvancedExample(specialProperty: \"Hello, World!\");\n    \/\/ If the property is empty, an InvalidArgumentException will be thrown\n    \/\/ $example = new AdvancedExample(specialProperty: \"\");\n} catch (InvalidArgumentException $e) {\n    echo \"Validation failed: \" . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<p>&#22312;&#22686;&#24378;&#30340;&#20363;&#23376;&#20013;&#65292;<code>ExampleAttribute<\/code>&#29616;&#22312;&#21487;&#20197;&#25509;&#21463;&#25253;&#21517;&#20102;&#12290;<code>description<\/code>&#25551;&#36848;&#23646;&#24615;&#30340;&#23383;&#31526;&#20018;&#12290;&#23427;&#20851;&#32852;&#30340;&#23646;&#24615;&#12290;<code>AdvancedExample<\/code>&#31867;&#26377;&#19968;&#20010;&#25509;&#21463;&#20540;&#30340;&#26500;&#36896;&#20989;&#25968;&#12290;<code>$specialProperty<\/code>And makes a call.<code>validateProperties<\/code>&#36827;&#34892;&#39564;&#35777;&#12290;<code>validateProperties<\/code>&#26041;&#27861;&#20351;&#29992;&#21453;&#23556;&#36941;&#21382;&#31867;&#30340;&#23646;&#24615;&#65292;&#33719;&#21462;&#30456;&#20851;&#23646;&#24615;&#24182;&#36827;&#34892;&#31616;&#21333;&#30340;&#26816;&#26597;&#20197;&#30830;&#20445;&#24102;&#26377;&#27880;&#35299;&#30340;&#23646;&#24615;&#12290;<code>ExampleAttribute<\/code>&#19981;&#26159;&#31354;&#30340;&#12290;<\/p>\n<p>&#36825;&#23637;&#31034;&#20102;&#22312;PHP 8.1&#20013;&#65292;&#23646;&#24615;&#21487;&#20197;&#29992;&#20110;&#26356;&#22810;&#21151;&#33021;&#65292;&#20363;&#22914;&#36816;&#34892;&#26102;&#30340; introspection&#65288;&#20803;&#25968;&#25454;&#27880;&#35299;&#65289;&#21644;&#39564;&#35777;&#65292;&#20197;&#21450;&#20854;&#20182;&#21487;&#33021;&#24615;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#22312;PHP&#20013;&#20026;&#31867;&#30340;&#23646;&#24615;&#23454;&#29616;&#31867;&#22411;&#22768;&#26126;&#21487;&#20197;&#25552;&#39640;&#20195;&#30721;&#36136;&#37327;&#65292;&#20415;&#20110;&#35843;&#35797;&#65292;&#24182;&#30830;&#20445;&#22312;&#25972;&#20010;&#24212;&#29992;&#20013;&#30340;&#31867;&#22411;&#19968;&#33268;&#24615;&#12290;&#36890;&#36807;&#25552;&#20379;&#30340;&#31034;&#20363;&#65292;&#25105;&#20204;&#30475;&#21040;&#20102;&#23450;&#20041;&#31867;&#23646;&#24615;&#30340;&#28436;&#21464;&#21644;&#28789;&#27963;&#24615;&#65292;&#20174;&#22522;&#26412;&#30340;&#31867;&#22411;&#22768;&#26126;&#21040;&#22797;&#26434;&#30340;&#12289;&#22810;&#26679;&#30340;&#24212;&#29992;&#22914;&#21487;&#31354;&#12289;&#32852;&#21512;&#21644;&#23646;&#24615;&#26631;&#35760;&#31867;&#22411;&#12290;&#25484;&#25569;&#36825;&#20123;&#21151;&#33021;&#21487;&#20197;&#22823;&#22823;&#25552;&#21319;&#20320;&#30340;PHP&#32534;&#30721;&#26631;&#20934;&#21644;&#35774;&#35745;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#31867;&#22411;&#22768;&#26126;&#65292;&#20063;&#31216;&#20026;&#31867;&#22411;&#25552;&#31034;&#65292;&#20801;&#35768;&#24320;&#21457;&#038;#32773..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-484","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\/484","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=484"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=484"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=484"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}