<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >类型运算符</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="运算符" HREF="language.operators.html"><LINK REL="PREVIOUS" TITLE="数组运算符" HREF="language.operators.array.html"><LINK REL="NEXT" TITLE="控制结构" HREF="language.control-structures.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.operators.array.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 15. 运算符</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.control-structures.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.operators.type" >类型运算符</A ></H1 ><P > PHP 只有一个类型运算符:<TT CLASS="literal" >instanceof</TT > 用来测定一个给定的对象,它的父对象或它们所实现的<A HREF="language.oop5.interfaces.html" >接口</A >是否来自指定的<A HREF="language.oop.html" >对象类</A >。 </P ><P > <TT CLASS="literal" >instanceof</TT > 运算符是 PHP 5 引进的。在此之前用 <A HREF="function.is-a.html" ><B CLASS="function" >is_a()</B ></A >,但是 <A HREF="function.is-a.html" ><B CLASS="function" >is_a()</B ></A > 已经过时了,最好用 <TT CLASS="literal" >instanceof</TT >。 </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4623" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">A </font><font color="#007700">{ }<br />class </font><font color="#0000BB">B </font><font color="#007700">{ }<br /><br /></font><font color="#0000BB">$thing </font><font color="#007700">= new </font><font color="#0000BB">A</font><font color="#007700">;<br /><br />if (</font><font color="#0000BB">$thing instanceof A</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'A'</font><font color="#007700">;<br />}<br />if (</font><font color="#0000BB">$thing instanceof B</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">'B'</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > 由于 <CODE CLASS="varname" >$thing</CODE > 是类型 A 的一个 <A HREF="language.types.object.html" ><B CLASS="type" >object</B ></A >,而不是 B 的,只有和类型 A 相符合的程序块被运行: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >A</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ><P > 参见 <A HREF="function.get-class.html" ><B CLASS="function" >get_class()</B ></A > 和 <A HREF="function.is-a.html" ><B CLASS="function" >is_a()</B ></A >。 </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.operators.array.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.control-structures.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >数组运算符</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.operators.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >控制结构</TD ></TR ></TABLE ></DIV ></BODY ></HTML >