Sophie

Sophie

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

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
>构造函数和析构函数</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="自动加载对象"
HREF="language.oop5.autoload.html"><LINK
REL="NEXT"
TITLE="访问控制"
HREF="language.oop5.visibility.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.autoload.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.visibility.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.oop5.decon"
>构造函数和析构函数</A
></H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.decon.constructor"
>构造函数</A
></H2
>void <B
CLASS="methodname"
>__construct</B
> ( [mixed args [, ...]] )<BR
></BR
><P
>&#13;   PHP 5 允行开发者在一个类中定义一个方法作为构造函数。具有构造函数的类会在每次创建对象时先调用此方法,所以非常适合在使用对象之前做一些初始化工作。
  </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    如果子类中定义了构造函数则不会暗中调用其父类的构造函数。要执行父类的构造函数,需要在子类的构造函数中调用
    <B
CLASS="function"
>parent::__construct()</B
>。
   </P
></BLOCKQUOTE
></DIV
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5760"
></A
><P
><B
>例 19-8. 使用新标准的构造函数</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">BaseClass </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__construct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"In BaseClass constructor\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class </font><font color="#0000BB">SubClass </font><font color="#007700">extends </font><font color="#0000BB">BaseClass </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__construct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">parent</font><font color="#007700">::</font><font color="#0000BB">__construct</font><font color="#007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"In SubClass constructor\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">BaseClass</font><font color="#007700">();<br /></font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">SubClass</font><font color="#007700">();<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;   为了实现向后兼容性,如果 PHP 5 在类中找不到 <B
CLASS="function"
>__construct()</B
>
   函数,它就会尝试寻找旧式的构造函数,也就是和类同名的函数。因此唯一会产生兼容性问题的情况是:类中已有一个名为
   <B
CLASS="function"
>__construct()</B
> 的方法,但它却又不是构造函数。
  </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.oop5.decon.destructor"
>析构函数</A
></H2
>void <B
CLASS="methodname"
>__destruct</B
> ( void  )<BR
></BR
><P
>&#13;   PHP 5 引入了析构函数的概念,这类似于其它面向对象的语言,如
   C++。析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。
  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN5773"
></A
><P
><B
>例 19-9. 析构函数示例</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">class </font><font color="#0000BB">MyDestructableClass </font><font color="#007700">{<br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__construct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"In constructor\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">name </font><font color="#007700">= </font><font color="#DD0000">"MyDestructableClass"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function </font><font color="#0000BB">__destruct</font><font color="#007700">() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </font><font color="#DD0000">"Destroying " </font><font color="#007700">. </font><font color="#0000BB">$this</font><font color="#007700">-&gt;</font><font color="#0000BB">name </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">MyDestructableClass</font><font color="#007700">();<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;   和构造函数一样,父类的析构函数不会被引擎暗中调用。要执行父类的析构函数,必须在子类的析构函数体中显式调用
   <B
CLASS="function"
>parent::__destruct()</B
>。
  </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    析构函数在脚本关闭时调用,此时所有的头信息已经发出。
   </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    试图在析构函数中抛出一个异常会导致致命错误。
   </P
></BLOCKQUOTE
></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="language.oop5.autoload.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.visibility.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.oop5.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>访问控制</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>