Sophie

Sophie

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

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
>Classes/Objects 类/对象函数</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="ccvs_void"
HREF="function.ccvs-void.html"><LINK
REL="NEXT"
TITLE="call_user_method_array"
HREF="function.call-user-method-array.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.ccvs-void.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.call-user-method-array.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.classobj"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>XII. Classes/Objects 类/对象函数</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN16693"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.intro"
>简介</A
></H1
><P
>&#13;    本类函数允许获取类和对象实例的信息。可以取得对象所属的类的名字,以及它的成员属性和方法。通过使用这些函数,不仅可以弄清楚一个对象类的全体成员,而且可以知道它的起源(例如,该对象类是哪个类的扩展)。
   </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.requirements"
>需求</A
></H1
><P
>要编译本扩展模块无需外部库文件。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.installation"
>安装</A
></H1
><P
>本扩展模块作为 PHP 内核的一部分,无需安装即可使用。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.configuration"
>运行时配置</A
></H1
><P
>本扩展模块在 <TT
CLASS="filename"
>php.ini</TT
> 中未定义任何配置选项。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.resources"
>资源类型</A
></H1
><P
>本扩展模块未定义任何资源类型。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.constants"
>预定义常量</A
></H1
><P
>本扩展模块未定义任何常量。</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.examples"
>范例</A
></H1
><P
>&#13;    在本例子中,先定义一个基础类和一个扩展类。基础类描述的是普通的蔬菜(Vegetable),它是否可食用(is_edible)以及它是什么颜色(what_color)。子类
    <CODE
CLASS="varname"
>Spinach</CODE
> 增加了“烹调”的方法(cook_it)和“获知它是否已被烹调了”的方法(is_cooked)。
   </P
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN16718"
></A
><P
><B
>例 1. classes.inc</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="#FF8000">// 基类及其成员属性与方法<br /></font><font color="#007700">class </font><font color="#0000BB">Vegetable </font><font color="#007700">{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$edible</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$color</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">Vegetable</font><font color="#007700">(</font><font color="#0000BB">$edible</font><font color="#007700">, </font><font color="#0000BB">$color</font><font color="#007700">=</font><font color="#DD0000">"green"</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">edible </font><font color="#007700">= </font><font color="#0000BB">$edible</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">color </font><font color="#007700">= </font><font color="#0000BB">$color</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">is_edible</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">edible</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">what_color</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">color</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />} </font><font color="#FF8000">// Vegetable 类定义结束<br /><br />// 扩展基类<br /></font><font color="#007700">class </font><font color="#0000BB">Spinach </font><font color="#007700">extends </font><font color="#0000BB">Vegetable </font><font color="#007700">{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;var </font><font color="#0000BB">$cooked </font><font color="#007700">= </font><font color="#0000BB">false</font><font color="#007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">Spinach</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">Vegetable</font><font color="#007700">(</font><font color="#0000BB">true</font><font color="#007700">, </font><font color="#DD0000">"green"</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">cook_it</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">cooked </font><font color="#007700">= </font><font color="#0000BB">true</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">is_cooked</font><font color="#007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">cooked</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />} </font><font color="#FF8000">// Spinach 类定义结束<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    接着,从这些对象中创建两个对象实例,打印出它们的相关信息,包括它们的起源。还定义了一些实用函数,它们主要是为了让变量的输出好看些。
   </P
><P
>&#13;    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN16723"
></A
><P
><B
>例 2. test_script.php</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
&lt;pre&gt;<br /><font color="#0000BB">&lt;?php<br /><br /></font><font color="#007700">include </font><font color="#DD0000">"classes.inc"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// utility functions<br /><br /></font><font color="#007700">function </font><font color="#0000BB">print_vars</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">get_object_vars</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">) as </font><font color="#0000BB">$prop </font><font color="#007700">=&gt; </font><font color="#0000BB">$val</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"</font><font color="#007700">\t</font><font color="#DD0000">$prop = $val</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">print_methods</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$arr </font><font color="#007700">= </font><font color="#0000BB">get_class_methods</font><font color="#007700">(</font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach (</font><font color="#0000BB">$arr </font><font color="#007700">as </font><font color="#0000BB">$method</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"</font><font color="#007700">\t</font><font color="#DD0000">function $method()</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">class_parentage</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">, </font><font color="#0000BB">$class</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">is_subclass_of</font><font color="#007700">(</font><font color="#0000BB">$GLOBALS</font><font color="#007700">[</font><font color="#0000BB">$obj</font><font color="#007700">], </font><font color="#0000BB">$class</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Object $obj belongs to class " </font><font color="#007700">. </font><font color="#0000BB">get_class</font><font color="#007700">($</font><font color="#0000BB">$obj</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">" a subclass of $class</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Object $obj does not belong to a subclass of $class</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#FF8000">// instantiate 2 objects<br /><br /></font><font color="#0000BB">$veggie </font><font color="#007700">= new </font><font color="#0000BB">Vegetable</font><font color="#007700">(</font><font color="#0000BB">true</font><font color="#007700">, </font><font color="#DD0000">"blue"</font><font color="#007700">);<br /></font><font color="#0000BB">$leafy </font><font color="#007700">= new </font><font color="#0000BB">Spinach</font><font color="#007700">();<br /><br /></font><font color="#FF8000">// print out information about objects<br /></font><font color="#007700">echo </font><font color="#DD0000">"veggie: CLASS " </font><font color="#007700">. </font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$veggie</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />echo </font><font color="#DD0000">"leafy: CLASS " </font><font color="#007700">. </font><font color="#0000BB">get_class</font><font color="#007700">(</font><font color="#0000BB">$leafy</font><font color="#007700">);<br />echo </font><font color="#DD0000">", PARENT " </font><font color="#007700">. </font><font color="#0000BB">get_parent_class</font><font color="#007700">(</font><font color="#0000BB">$leafy</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// show veggie properties<br /></font><font color="#007700">echo </font><font color="#DD0000">"\nveggie: Properties\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_vars</font><font color="#007700">(</font><font color="#0000BB">$veggie</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// and leafy methods<br /></font><font color="#007700">echo </font><font color="#DD0000">"\nleafy: Methods\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_methods</font><font color="#007700">(</font><font color="#0000BB">$leafy</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"\nParentage:\n"</font><font color="#007700">;<br /></font><font color="#0000BB">class_parentage</font><font color="#007700">(</font><font color="#DD0000">"leafy"</font><font color="#007700">, </font><font color="#DD0000">"Spinach"</font><font color="#007700">);<br /></font><font color="#0000BB">class_parentage</font><font color="#007700">(</font><font color="#DD0000">"leafy"</font><font color="#007700">, </font><font color="#DD0000">"Vegetable"</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;<br /></font>&lt;/pre&gt;</font>
</code></TD
></TR
></TABLE
><P
>&#13;      上边例子中重点要注意的是对象
      <CODE
CLASS="varname"
>$leafy</CODE
> 是
      <B
CLASS="classname"
>Spinach</B
> 类的一个实例,而
      <B
CLASS="classname"
>Spinach</B
> 类是
      <B
CLASS="classname"
>Vegetable</B
> 类的一个子类,因此上边脚本的最后部分将会输出:
     </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>目录</B
></DT
><DT
><A
HREF="function.call-user-method-array.html"
>call_user_method_array</A
>&nbsp;--&nbsp;
   调用一个用户方法,同时传递参数数组(已废弃)
  </DT
><DT
><A
HREF="function.call-user-method.html"
>call_user_method</A
>&nbsp;--&nbsp;
   对特定对象调用用户方法(已废弃)
  </DT
><DT
><A
HREF="function.class-exists.html"
>class_exists</A
>&nbsp;--&nbsp;检查类是否已定义</DT
><DT
><A
HREF="function.get-class-methods.html"
>get_class_methods</A
>&nbsp;--&nbsp;返回由类的方法名组成的数组</DT
><DT
><A
HREF="function.get-class-vars.html"
>get_class_vars</A
>&nbsp;--&nbsp;返回由类的默认属性组成的数组</DT
><DT
><A
HREF="function.get-class.html"
>get_class</A
>&nbsp;--&nbsp;返回对象的类名</DT
><DT
><A
HREF="function.get-declared-classes.html"
>get_declared_classes</A
>&nbsp;--&nbsp;返回由已定义类的名字所组成的数组</DT
><DT
><A
HREF="function.get-declared-interfaces.html"
>get_declared_interfaces</A
>&nbsp;--&nbsp;返回一个数组包含所有已声明的接口</DT
><DT
><A
HREF="function.get-object-vars.html"
>get_object_vars</A
>&nbsp;--&nbsp;返回由对象属性组成的关联数组</DT
><DT
><A
HREF="function.get-parent-class.html"
>get_parent_class</A
>&nbsp;--&nbsp;返回对象或类的父类名</DT
><DT
><A
HREF="function.interface-exists.html"
>interface_exists</A
>&nbsp;--&nbsp;检查接口是否已被定义</DT
><DT
><A
HREF="function.is-a.html"
>is_a</A
>&nbsp;--&nbsp;如果对象属于该类或该类是此对象的父类则返回 <TT
CLASS="constant"
><B
>TRUE</B
></TT
></DT
><DT
><A
HREF="function.is-subclass-of.html"
>is_subclass_of</A
>&nbsp;--&nbsp;如果此对象是该类的子类,则返回 <TT
CLASS="constant"
><B
>TRUE</B
></TT
></DT
><DT
><A
HREF="function.method-exists.html"
>method_exists</A
>&nbsp;--&nbsp;检查类的方法是否存在</DT
><DT
><A
HREF="function.property-exists.html"
>property_exists</A
>&nbsp;--&nbsp;检查对象或类是否具有该属性</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.ccvs-void.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.call-user-method-array.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>ccvs_void</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>call_user_method_array</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>