Sophie

Sophie

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

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 property and method call overloading</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="funcref.html"><LINK
REL="PREVIOUS"
TITLE="deaggregate"
HREF="function.deaggregate.html"><LINK
REL="NEXT"
TITLE="overload"
HREF="function.overload.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="reference"
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="function.deaggregate.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.overload.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.overload"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>CVI. Object property and method call overloading</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN155830"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.intro"
>简介</A
></H1
><P
>&#13;     The purpose of this extension is to allow overloading of object
     property access and method calls. Only one function is defined
     in this extension, <A
HREF="function.overload.html"
><B
CLASS="function"
>overload()</B
></A
> which
     takes the name of the class that should have this functionality
     enabled. The class named has to define appropriate methods if
     it wants to have this functionality: <TT
CLASS="literal"
>__get()</TT
>,
     <TT
CLASS="literal"
>__set()</TT
> and <TT
CLASS="literal"
>__call()</TT
>
     respectively for getting/setting a property, or calling a method.
     This way overloading can be selective. Inside these handler
     functions the overloading is disabled so you can access object
     properties normally.
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>警告</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>本扩展模块是<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>实验性</I
></SPAN
>的。本模块的行为,包括其函数的名称以及其它任何关于此模块的文档可能会在没有通知的情况下随
PHP 以后的发布而改变。使用本扩展模块风险自担。</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>警告</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;      This extension is not a part of PHP 5. PHP 5 supports <TT
CLASS="literal"
>__get()</TT
>, <TT
CLASS="literal"
>__set()</TT
> and
      <TT
CLASS="literal"
>__call()</TT
> natively. See the <A
HREF="language.oop5.overloading.html"
>Overloading in PHP 5</A
> page
      for more information.
     </P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.requirements"
>需求</A
></H1
><P
>要编译本扩展模块无需外部库文件。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.installation"
>安装</A
></H1
><P
>&#13;  In order to use these functions, you must compile
  PHP with the <CODE
CLASS="option"
>--enable-overload</CODE
> option.
  Starting with PHP 4.3.0 this extension is enabled by default. You can
  disable overload support with
  <CODE
CLASS="option"
>--disable--overload</CODE
>.
 </P
><P
> <TT
CLASS="literal"
>PHP</TT
> 的 Windows
版本已经内置该扩展模块的支持。无需加载任何附加扩展库即可使用这些函数。</P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
   Builtin support for overload is available with PHP 4.3.0.
  </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.configuration"
>运行时配置</A
></H1
><P
>本扩展模块在 <TT
CLASS="filename"
>php.ini</TT
> 中未定义任何配置选项。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.resources"
>资源类型</A
></H1
><P
>本扩展模块未定义任何资源类型。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.constants"
>预定义常量</A
></H1
><P
>本扩展模块未定义任何常量。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.examples"
>范例</A
></H1
><P
>&#13;     Some simple examples on using the <A
HREF="function.overload.html"
><B
CLASS="function"
>overload()</B
></A
>
     function:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN155873"
></A
><P
><B
>例 1. Overloading a PHP class</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br /></font><font color="#007700">class </font><font color="#0000BB">OO </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">111</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$elem </font><font color="#007700">= array(</font><font color="#DD0000">'b' </font><font color="#007700">=&gt; </font><font color="#0000BB">9</font><font color="#007700">, </font><font color="#DD0000">'c' </font><font color="#007700">=&gt; </font><font color="#0000BB">42</font><font color="#007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Callback method for getting a property<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">__get</font><font color="#007700">(</font><font color="#0000BB">$prop_name</font><font color="#007700">, &amp;</font><font color="#0000BB">$prop_value</font><font color="#007700">) <br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (isset(</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">elem</font><font color="#007700">[</font><font color="#0000BB">$prop_name</font><font color="#007700">])) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$prop_value </font><font color="#007700">= </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">elem</font><font color="#007700">[</font><font color="#0000BB">$prop_name</font><font color="#007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">false</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Callback method for setting a property<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">function </font><font color="#0000BB">__set</font><font color="#007700">(</font><font color="#0000BB">$prop_name</font><font color="#007700">, </font><font color="#0000BB">$prop_value</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">elem</font><font color="#007700">[</font><font color="#0000BB">$prop_name</font><font color="#007700">] = </font><font color="#0000BB">$prop_value</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">// Here we overload the OO object<br /></font><font color="#0000BB">overload</font><font color="#007700">(</font><font color="#DD0000">'OO'</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$o </font><font color="#007700">= new </font><font color="#0000BB">OO</font><font color="#007700">;<br />echo </font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">o</font><font color="#007700">-&gt;</font><font color="#DD0000">a: $o</font><font color="#007700">-&gt;</font><font color="#DD0000">a</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">; </font><font color="#FF8000">// print: $o-&gt;a: 111<br /></font><font color="#007700">echo </font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">o</font><font color="#007700">-&gt;</font><font color="#DD0000">b: $o</font><font color="#007700">-&gt;</font><font color="#DD0000">b</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">; </font><font color="#FF8000">// print: $o-&gt;b: 9<br /></font><font color="#007700">echo </font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">o</font><font color="#007700">-&gt;</font><font color="#DD0000">c: $o</font><font color="#007700">-&gt;</font><font color="#DD0000">c</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">; </font><font color="#FF8000">// print: $o-&gt;c: 42<br /></font><font color="#007700">echo </font><font color="#DD0000">"</font><font color="#007700">\$</font><font color="#DD0000">o</font><font color="#007700">-&gt;</font><font color="#DD0000">d: $o</font><font color="#007700">-&gt;</font><font color="#DD0000">d</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">; </font><font color="#FF8000">// print: $o-&gt;d:<br /><br />// add a new item to the $elem array in OO<br /></font><font color="#0000BB">$o</font><font color="#007700">-&gt;</font><font color="#0000BB">x </font><font color="#007700">= </font><font color="#0000BB">56</font><font color="#007700">; <br /><br /></font><font color="#FF8000">// instantiate stdclass (it is built-in in PHP 4)<br />// $val is not overloaded!<br /></font><font color="#0000BB">$val </font><font color="#007700">= new </font><font color="#0000BB">stdclass</font><font color="#007700">;<br /></font><font color="#0000BB">$val</font><font color="#007700">-&gt;</font><font color="#0000BB">prop </font><font color="#007700">= </font><font color="#0000BB">555</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Set "a" to be an array with the $val object in it<br />// But __set() will put this in the $elem array<br /></font><font color="#0000BB">$o</font><font color="#007700">-&gt;</font><font color="#0000BB">a </font><font color="#007700">= array(</font><font color="#0000BB">$val</font><font color="#007700">);<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$o</font><font color="#007700">-&gt;</font><font color="#0000BB">a</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]-&gt;</font><font color="#0000BB">prop</font><font color="#007700">);<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>目录</B
></DT
><DT
><A
HREF="function.overload.html"
>overload</A
>&nbsp;--&nbsp;Enable property and method call overloading for a class</DT
></DL
></DIV
></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="function.deaggregate.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="function.overload.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>deaggregate</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>overload</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>