<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Type Hinting</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="类与对象(PHP 5)" HREF="language.oop5.html"><LINK REL="PREVIOUS" TITLE="Reflection" HREF="language.oop5.reflection.html"><LINK REL="NEXT" TITLE="异常处理" HREF="language.exceptions.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.oop5.reflection.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 19. 类与对象(PHP 5)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.exceptions.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop5.typehinting" >Type Hinting</A ></H1 ><P > PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6271" ></A ><P ><B >例 19-41. Type Hinting examples</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// An example class<br /></font><font color="#007700">class </font><font color="#0000BB">MyClass<br /></font><font color="#007700">{<br /> </font><font color="#FF8000">/**<br /> * A test function<br /> *<br /> * First parameter must be an object of type OtherClass<br /> */<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">test</font><font color="#007700">(</font><font color="#0000BB">OtherClass $otherclass</font><font color="#007700">) {<br /> echo </font><font color="#0000BB">$otherclass</font><font color="#007700">-></font><font color="#0000BB">var</font><font color="#007700">;<br /> }<br /><br /><br /> </font><font color="#FF8000">/**<br /> * Another test function<br /> *<br /> * First parameter must be an array<br /> */<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">test_array</font><font color="#007700">(array </font><font color="#0000BB">$input_array</font><font color="#007700">) {<br /> </font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$input_array</font><font color="#007700">);<br /> }<br />}<br /><br /></font><font color="#FF8000">// Another example class<br /></font><font color="#007700">class </font><font color="#0000BB">OtherClass </font><font color="#007700">{<br /> </font><font color="#0000BB">public $var </font><font color="#007700">= </font><font color="#DD0000">'Hello World'</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > Failing to satisfy the type hint results in a fatal error. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// An instance of each class<br /></font><font color="#0000BB">$myclass </font><font color="#007700">= new </font><font color="#0000BB">MyClass</font><font color="#007700">;<br /></font><font color="#0000BB">$otherclass </font><font color="#007700">= new </font><font color="#0000BB">OtherClass</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Fatal Error: Argument 1 must be an object of class OtherClass<br /></font><font color="#0000BB">$myclass</font><font color="#007700">-></font><font color="#0000BB">test</font><font color="#007700">(</font><font color="#DD0000">'hello'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Fatal Error: Argument 1 must be an instance of OtherClass<br /></font><font color="#0000BB">$foo </font><font color="#007700">= new </font><font color="#0000BB">stdClass</font><font color="#007700">;<br /></font><font color="#0000BB">$myclass</font><font color="#007700">-></font><font color="#0000BB">test</font><font color="#007700">(</font><font color="#0000BB">$foo</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Fatal Error: Argument 1 must not be null<br /></font><font color="#0000BB">$myclass</font><font color="#007700">-></font><font color="#0000BB">test</font><font color="#007700">(</font><font color="#0000BB">null</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Works: Prints Hello World<br /></font><font color="#0000BB">$myclass</font><font color="#007700">-></font><font color="#0000BB">test</font><font color="#007700">(</font><font color="#0000BB">$otherclass</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Fatal Error: Argument 1 must be an array<br /></font><font color="#0000BB">$myclass</font><font color="#007700">-></font><font color="#0000BB">test_array</font><font color="#007700">(</font><font color="#DD0000">'a string'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Works: Prints the array<br /></font><font color="#0000BB">$myclass</font><font color="#007700">-></font><font color="#0000BB">test_array</font><font color="#007700">(array(</font><font color="#DD0000">'a'</font><font color="#007700">, </font><font color="#DD0000">'b'</font><font color="#007700">, </font><font color="#DD0000">'c'</font><font color="#007700">));<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > Type hinting also works with functions: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// An example class<br /></font><font color="#007700">class </font><font color="#0000BB">MyClass </font><font color="#007700">{<br /> </font><font color="#0000BB">public $var </font><font color="#007700">= </font><font color="#DD0000">'Hello World'</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">/**<br /> * A test function<br /> *<br /> * First parameter must be an object of type MyClass<br /> */<br /></font><font color="#007700">function </font><font color="#0000BB">MyFunction </font><font color="#007700">(</font><font color="#0000BB">MyClass $foo</font><font color="#007700">) {<br /> echo </font><font color="#0000BB">$foo</font><font color="#007700">-></font><font color="#0000BB">var</font><font color="#007700">;<br />}<br /><br /></font><font color="#FF8000">// Works<br /></font><font color="#0000BB">$myclass </font><font color="#007700">= new </font><font color="#0000BB">MyClass</font><font color="#007700">;<br /></font><font color="#0000BB">MyFunction</font><font color="#007700">(</font><font color="#0000BB">$myclass</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > Type Hints can only be of the <A HREF="language.types.object.html" ><B CLASS="type" >object</B ></A > and <A HREF="language.types.array.html" ><B CLASS="type" >array</B ></A > (since PHP 5.1) type. Traditional type hinting with <A HREF="language.types.integer.html" ><B CLASS="type" >int</B ></A > and <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A > isn't supported. </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.oop5.reflection.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.exceptions.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Reflection</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop5.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >异常处理</TD ></TR ></TABLE ></DIV ></BODY ></HTML >