{"id":448,"date":"2025-06-10T20:35:31","date_gmt":"2025-06-10T12:35:31","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/448.html"},"modified":"2025-06-10T20:35:31","modified_gmt":"2025-06-10T12:35:31","slug":"php-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-instanceof-%e8%bf%90%e7%ae%97%e7%ac%a6","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/448.html","title":{"rendered":"PHP: \u5982\u4f55\u4f7f\u7528 instanceof \u8fd0\u7b97\u7b26"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#20449;&#24687;&#19981;&#23436;&#25972;&#65292;&#35831;&#25552;&#20379;&#26356;&#22810;&#20449;&#24687;&#12290;<code>instanceof<\/code>&#22312;PHP&#20013;&#65292;&#25805;&#20316;&#31526;&#31867;&#65288;operator class&#65289;&#26159;&#19968;&#31181;&#24378;&#22823;&#30340;&#24037;&#20855;&#65292;&#29992;&#20110;&#30830;&#23450;&#32473;&#23450;&#30340;&#23545;&#35937;&#26159;&#21542;&#20026;&#29305;&#23450;&#31867;&#30340;&#23454;&#20363;&#25110;&#32487;&#25215;&#33258;&#29305;&#23450;&#31867;&#30340;&#23454;&#20363;&#12290;&#23427;&#36824;&#21487;&#20197;&#26816;&#26597;&#23545;&#35937;&#26159;&#21542;&#23454;&#29616;&#20102;&#19968;&#20010;&#25509;&#21475;&#12290;<\/p>\n<h2>&#29702;&#35299;<code>instanceof<\/code><\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#38382;&#39064;&#19981;&#23436;&#25972;&#65292;&#35831;&#24744;&#37325;&#26032;&#25551;&#36848;&#19968;&#19979;&#12290;<code>instanceof<\/code>PHP&#20013;&#30340;&#25805;&#20316;&#31526;&#29992;&#20110;&#22312;&#24067;&#23572;&#19978;&#19979;&#25991;&#20013;&#30830;&#23450;&#29305;&#23450;&#23545;&#35937;&#26159;&#21542;&#23454;&#20363;&#21270;&#20110;&#25351;&#23450;&#30340;&#31867;&#25110;&#25509;&#21475;&#12290;&#23427;&#36820;&#22238;<code>true<\/code>&#22914;&#26524;&#24038;&#20391;&#30340;&#23545;&#35937;&#23454;&#20363;&#21270;&#20102;&#21491;&#20391;&#30340;&#31867;&#25110;&#25509;&#21475;&#65292;&#36820;&#22238;true&#65307;&#21542;&#21017;&#36820;&#22238;false&#12290;&#22522;&#26412;&#35821;&#27861;&#20026;&#65306;<\/p>\n<pre><code>$object instanceof ClassName<\/code><\/pre>\n<p>&#36825;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code>class MyClass {}\n\n$obj = new MyClass();\n\nif ($obj instanceof MyClass) {\n    echo 'The object is an instance of MyClass.';\n}\n<\/code><\/pre>\n<h2>&#27491;&#22312;&#26816;&#26597;&#25509;&#21475;&#23454;&#29616;&#12290;<\/h2>\n<p>PHP &#24320;&#21457;&#20154;&#21592;&#36890;&#24120;&#20351;&#29992;&#25509;&#21475;&#26469;&#23450;&#20041;&#19968;&#32452;&#20844;&#20849;&#26041;&#27861;&#65292;&#36825;&#20123;&#26041;&#27861;&#21487;&#20197;&#34987;&#19981;&#21516;&#30340;&#31867;&#23454;&#29616;&#12290;<code>instanceof<\/code>&#25805;&#20316;&#21592;&#21487;&#20197;&#25353;&#29031;&#20197;&#19979;&#26041;&#24335;&#26816;&#26597;&#19968;&#20010;&#31867;&#26159;&#21542;&#23454;&#29616;&#20102;&#26576;&#20010;&#29305;&#23450;&#30340;&#25509;&#21475;&#65306;<\/p>\n<pre><code>interface MyInterface {}\n\nclass MyClass implements MyInterface {}\n\n$obj = new MyClass();\n\nif ($obj instanceof MyInterface) {\n    echo 'The object implements MyInterface.';\n}\n<\/code><\/pre>\n<h2>&#22312;&#32487;&#25215;&#24615;&#20013;&#20351;&#29992;instanceof&#25805;&#20316;&#31526;&#12290;<\/h2>\n<p>&#24403;&#31867;&#32487;&#25215;&#20854;&#20182;&#31867;&#26102;&#65292;&#20250;&#25191;&#34892;&#20197;&#19979;&#25805;&#20316;&#65306;&#39318;&#20808;&#26816;&#26597;&#31867;&#21517;&#26159;&#21542;&#20026;&#22522;&#31867;&#30340;&#23376;&#31867;&#65307;&#20854;&#27425;&#65292;&#26816;&#26597;&#22522;&#31867;&#20013;&#30340;&#25152;&#26377;&#25104;&#21592;&#65288;&#21253;&#25324;&#26500;&#36896;&#20989;&#25968;&#21644;&#26512;&#26500;&#20989;&#25968;&#65289;&#26159;&#21542;&#22312;&#27966;&#29983;&#31867;&#20013;&#34987;&#37325;&#26032;&#23450;&#20041;&#25110;&#37325;&#36733;&#65307;&#26368;&#21518;&#65292;&#26816;&#26597;&#27966;&#29983;&#31867;&#26159;&#21542;&#26377;&#26032;&#30340;&#25104;&#21592;&#21464;&#37327;&#25110;&#26041;&#27861;&#28155;&#21152;&#21040;&#22522;&#31867;&#20013;&#12290;&#22914;&#26524;&#28385;&#36275;&#36825;&#20123;&#26465;&#20214;&#65292;&#21017;&#21487;&#20197;&#36827;&#34892;&#31867;&#30340;&#32487;&#25215;&#12290;<code>instanceof<\/code>&#36816;&#33829;&#21830;&#21487;&#20197;&#30830;&#35748;&#19968;&#20010;&#23545;&#35937;&#26159;&#21542;&#26159;&#20854;&#29238;&#31867;&#30340;&#19968;&#20010;&#23454;&#20363;&#65306;<\/p>\n<pre><code>class ParentClass {}\n\nclass ChildClass extends ParentClass {}\n\n$obj = new ChildClass();\n\nif ($obj instanceof ParentClass) {\n    echo 'The object is an instance of ParentClass or a derived class.';\n}\n<\/code><\/pre>\n<h2>&#20351;&#29992; instanceof &#19982;&#25277;&#35937;&#31867;<\/h2>\n<p>&#25277;&#35937;&#31867;&#22312;PHP&#20013;&#29992;&#20110;&#21019;&#24314;&#20855;&#20307;&#30340;&#31867;&#12290;&#20197;&#19979;&#26159;&#26816;&#26597;&#23454;&#20363;&#26159;&#21542;&#23646;&#20110;&#20174;&#25277;&#35937;&#31867;&#27966;&#29983;&#30340;&#31867;&#30340;&#26041;&#27861;&#65306;<\/p>\n<pre><code>abstract class AbstractClass {}\n\nclass ConcreteClass extends AbstractClass {}\n\n$obj = new ConcreteClass();\n\nif ($obj instanceof AbstractClass) {\n    echo 'The object is derived from AbstractClass.';\n}\n<\/code><\/pre>\n<h2>&#21160;&#24577;&#26816;&#26597;&#19982;&#21464;&#37327;&#20351;&#29992;<\/h2>\n<p>&#19968;&#20010;&#24378;&#22823;&#30340;&#26041;&#38754;&#26159;<code>instanceof<\/code>&#23427;&#30340;&#21464;&#37327;&#33021;&#21147;&#21487;&#20197;&#19982;&#21253;&#21547;&#31867;&#21517;&#30340;&#21464;&#37327;&#19968;&#36215;&#24037;&#20316;&#12290;&#20363;&#22914;&#65292;&#36825;&#35753;&#20320;&#33021;&#22815;&#39564;&#35777;&#23545;&#35937;&#23646;&#20110;&#30001;&#23383;&#31526;&#20018;&#21464;&#37327;&#25351;&#23450;&#30340;&#31867;&#65306;<\/p>\n<pre><code>$className = 'MyClass';\n$obj = new MyClass();\n\nif ($obj instanceof $className) {\n    echo 'The object is an instance of ' . $className . '.';\n}\n<\/code><\/pre>\n<h2>&#22788;&#29702;&#38169;&#35823;<\/h2>\n<p>&#19981;&#24403;&#20351;&#29992;&#24037;&#20855;&#30340;<code>instanceof<\/code>&#25805;&#20316;&#31526;&#21487;&#33021;&#23548;&#33268;&#20195;&#30721;&#20013;&#30340;&#38169;&#35823;&#12290;&#22312;&#20351;&#29992;&#21464;&#37327;&#20043;&#21069;&#65292;&#30830;&#20445;&#35813;&#21464;&#37327;&#30830;&#23454;&#26159;&#23545;&#35937;&#26159;&#38750;&#24120;&#37325;&#35201;&#30340;&#12290;<code>instanceof<\/code>&#36825;&#21487;&#20197;&#36890;&#36807;&#20197;&#19979;&#27493;&#39588;&#23436;&#25104;&#65306;<\/p>\n<pre><code>$var = null;\n\nif (is_object($var) &amp;&amp; $var instanceof MyClass) {\n    echo 'The variable is an object of MyClass.';\n} else {\n    echo 'The variable is not an object or not an instance of MyClass.';\n}\n<\/code><\/pre>\n<h2>&#22312;Action&#20013;&#23454;&#29616;&#22810;&#24577;&#24615;&#12290;<\/h2>\n<p>&#22810;&#24577;&#24615;&#26159;&#38754;&#21521;&#23545;&#35937;&#32534;&#31243;&#30340;&#26680;&#24515;&#27010;&#24565;&#20043;&#19968;&#65292;&#25351;&#30340;&#26159;&#21516;&#19968;&#20010;&#25805;&#20316;&#21487;&#20197;&#34987;&#19981;&#21516;&#30340;&#25968;&#25454;&#31867;&#22411;&#35843;&#29992;&#12290;<code>instanceof<\/code>&#36816;&#33829;&#21830;&#21487;&#20197;&#24110;&#21161;&#31649;&#29702;&#12290;&#20197;&#19979;&#26159;&#22914;&#20309;&#20351;&#29992;&#30340;&#31034;&#20363;&#12290;<code>instanceof<\/code>&#22312;&#22810;&#24577;&#24615;&#19978;&#19979;&#25991;&#20013;&#65292;&#19968;&#20010;&#20989;&#25968;&#25509;&#21463;&#19981;&#21516;&#31867;&#22411;&#30340;&#23545;&#35937;&#65306;<\/p>\n<pre><code>function process($obj) {\n    if ($obj instanceof ClassA) {\n        \/\/ Process $obj as an instance of ClassA\n    } elseif ($obj instanceof ClassB) {\n        \/\/ Process $obj as an instance of ClassB\n    } elseif ($obj instanceof ClassC) {\n        \/\/ Process $obj as an instance of ClassC\n    } else {\n        throw new InvalidArgumentException('Unsupported object type.');\n    }\n}\n<\/code><\/pre>\n<h2>&#22788;&#29702;&#25509;&#21475;&#21644;&#29305;&#36136;<\/h2>\n<p>&#23601;&#20687;&#29677;&#32423;&#19968;&#26679;&#65292;&#30340;&#12290;<code>instanceof<\/code>&#25805;&#20316;&#31526;&#21487;&#20197;&#19982;&#25509;&#21475;&#21644;&#29305;&#24615;&#19968;&#36215;&#20351;&#29992;&#65292;&#20197;&#30830;&#20445;&#23545;&#35937;&#23454;&#29616;&#26576;&#20123;&#34892;&#20026;&#65306;<\/p>\n<pre><code>trait MyTrait {}\nclass MyUsefulClass { use MyTrait; }\n\n$obj = new MyUsefulClass();\n\nif ($obj instanceof MyTrait) {\n    echo 'The object uses MyTrait.';\n}\n<\/code><\/pre>\n<h2>&#39640;&#32423;&#29992;&#27861;&#65306;&#32467;&#21512;&#31867;&#22411;&#22768;&#26126;<\/h2>\n<p>&#22312;&#36816;&#34892;&#26102;&#30340;&#21160;&#24577;&#26816;&#26597;&#20043;&#22806;&#65292;PHP&#30340;&#31867;&#22411;&#22768;&#26126;&#20063;&#21487;&#20197;&#19982;&#20043;&#32467;&#21512;&#20351;&#29992;&#12290;<code>instanceof<\/code>&#20026;&#20102;&#26356;&#24378;&#22823;&#30340;&#31867;&#22411;&#26816;&#26597;&#65306;<\/p>\n<pre><code>function printClassNameIfNeeded(object $obj) {\n    if ($obj instanceof MyClass) {\n        echo get_class($obj);\n    }\n}\n<\/code><\/pre>\n<h2>&#32467;&#35770;<\/h2>\n<p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#26126;&#30333;&#24744;&#30340;&#38382;&#39064;&#12290;<code>instanceof<\/code>&#25805;&#20316;&#31526;&#22312;PHP&#20013;&#23545;&#31649;&#29702;&#23545;&#35937;&#31867;&#22411;&#33267;&#20851;&#37325;&#35201;&#12290;&#23427;&#30830;&#20445;&#23545;&#35937;&#27491;&#30830;&#22320;&#23454;&#20363;&#21270;&#20026;&#31867;&#25110;&#25509;&#21475;&#65292;&#20174;&#32780;&#22686;&#24378;&#24212;&#29992;&#31243;&#24207;&#30340;&#20581;&#22766;&#24615;&#21644;&#31867;&#22411;&#23433;&#20840;&#24615;&#12290;&#22312;&#20351;&#29992;&#21464;&#37327;&#20043;&#21069;&#65292;&#35831;&#30830;&#35748;&#23427;&#20204;&#30830;&#23454;&#26159;&#23545;&#35937;&#12290;<code>instanceof<\/code>&#20026;&#20102;&#36991;&#20813;&#28508;&#22312;&#30340;&#38169;&#35823;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#20449;&#24687;&#19981;&#23436;&#25972;&#65292;&#35831;&#25552;&#20379;&#26356;&#22810;&#038;#20449..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-448","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\/448","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=448"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=448"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=448"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}