Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 11983

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Object Interfaces</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="Class Abstraction"
HREF="language.oop5.abstract.html"><LINK
REL="NEXT"
TITLE="Overloading"
HREF="language.oop5.overloading.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.abstract.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.oop5.overloading.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop5.interfaces"
>Object Interfaces</A
></H1
><P
>&#13;   Object interfaces allow you to create code which specifies which methods a
   class must implement, without having to define how these methods are
   handled.
  </P
><P
>&#13;   Interfaces are defined using the interface keyword, in the same way as a
   standard class, but without any of the methods having their contents
   defined.
  </P
><P
>&#13;   All methods declared in an interface must be public, this is the nature of an
   interface.
  </P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="AEN5872"
><TT
CLASS="literal"
>implements</TT
></A
></H2
><P
>&#13;    To implement an interface, the <TT
CLASS="literal"
>implements</TT
> operator is used.
    All methods in the interface must be implemented within a class; failure to do
    so will result in a fatal error. Classes may implement more than one interface
    if desired by separating each interface with a comma.
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
     A class cannot implement two interfaces that share function names, since
     it would cause ambiguity.
    </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="AEN5879"
>范例</A
></H2
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5881"
></A
><P
><B
>例 19-19. Interface example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Declare the interface 'iTemplate'<br /></font><font color="#0000BB">interface iTemplate<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">setVariable</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$var</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">getHtml</font><font color="#007700">(</font><font color="#0000BB">$template</font><font color="#007700">);<br />}<br /><br /></font><font color="#FF8000">// Implement the interface<br />// This will work<br /></font><font color="#007700">class </font><font color="#0000BB">Template implements iTemplate<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">private $vars </font><font color="#007700">= array();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">setVariable</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$var</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">vars</font><font color="#007700">[</font><font color="#0000BB">$name</font><font color="#007700">] = </font><font color="#0000BB">$var</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">getHtml</font><font color="#007700">(</font><font color="#0000BB">$template</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">vars </font><font color="#007700">as </font><font color="#0000BB">$name </font><font color="#007700">=&gt; </font><font color="#0000BB">$value</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$template </font><font color="#007700">= </font><font color="#0000BB">str_replace</font><font color="#007700">(</font><font color="#DD0000">'{' </font><font color="#007700">. </font><font color="#0000BB">$name </font><font color="#007700">. </font><font color="#DD0000">'}'</font><font color="#007700">, </font><font color="#0000BB">$value</font><font color="#007700">, </font><font color="#0000BB">$template</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /> <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$template</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">// This will not work<br />// Fatal error: Class BadTemplate contains 1 abstract methods<br />// and must therefore be declared abstract (iTemplate::getHtml)<br /></font><font color="#007700">class </font><font color="#0000BB">BadTemplate implements iTemplate<br /></font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">private $vars </font><font color="#007700">= array();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">setVariable</font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">, </font><font color="#0000BB">$var</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">vars</font><font color="#007700">[</font><font color="#0000BB">$name</font><font color="#007700">] = </font><font color="#0000BB">$var</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;     See also the <A
HREF="language.operators.type.html"
>instanceof</A
>
     operator.
   </P
></DIV
></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.abstract.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.oop5.overloading.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Class Abstraction</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop5.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Overloading</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>