<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Static Keyword</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.paamayim-nekudotayim.html"><LINK REL="NEXT" TITLE="Class Constants" HREF="language.oop5.constants.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.paamayim-nekudotayim.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.constants.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop5.static" >Static Keyword</A ></H1 ><P > Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can). </P ><P > The static declaration must be after the visibility declaration. For compatibility with PHP 4, if no <A HREF="language.oop5.visibility.html" >visibility</A > declaration is used, then the member or method will be treated as if it was declared as <TT CLASS="literal" >public</TT >. </P ><P > Because static methods are callable without an instance of the object created, the pseudo variable <CODE CLASS="varname" >$this</CODE > is not available inside the method declared as static. </P ><P > In fact <TT CLASS="literal" >static</TT > method calls are resolved at compile time. When using an explicit class name the method is already identified completely and no inheritance rules apply. If the call is done by <TT CLASS="literal" >self</TT > then <TT CLASS="literal" >self</TT > is translated to the current class, that is the class the code belongs to. Here also no inheritance rules apply. </P ><P > Static properties cannot be accessed through the object using the arrow operator ->. </P ><P > Calling non-static methods statically generates an E_STRICT level warning. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5840" ></A ><P ><B >例 19-15. Static member example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">Foo<br /></font><font color="#007700">{<br /> </font><font color="#0000BB">public </font><font color="#007700">static </font><font color="#0000BB">$my_static </font><font color="#007700">= </font><font color="#DD0000">'foo'</font><font color="#007700">;<br /><br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">staticValue</font><font color="#007700">() {<br /> return </font><font color="#0000BB">self</font><font color="#007700">::</font><font color="#0000BB">$my_static</font><font color="#007700">;<br /> }<br />}<br /><br />class </font><font color="#0000BB">Bar </font><font color="#007700">extends </font><font color="#0000BB">Foo<br /></font><font color="#007700">{<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">fooStatic</font><font color="#007700">() {<br /> return </font><font color="#0000BB">parent</font><font color="#007700">::</font><font color="#0000BB">$my_static</font><font color="#007700">;<br /> }<br />}<br /><br /><br />print </font><font color="#0000BB">Foo</font><font color="#007700">::</font><font color="#0000BB">$my_static </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$foo </font><font color="#007700">= new </font><font color="#0000BB">Foo</font><font color="#007700">();<br />print </font><font color="#0000BB">$foo</font><font color="#007700">-></font><font color="#0000BB">staticValue</font><font color="#007700">() . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />print </font><font color="#0000BB">$foo</font><font color="#007700">-></font><font color="#0000BB">my_static </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">; </font><font color="#FF8000">// Undefined "Property" my_static <br /><br />// $foo::my_static is not possible<br /><br /></font><font color="#007700">print </font><font color="#0000BB">Bar</font><font color="#007700">::</font><font color="#0000BB">$my_static </font><font color="#007700">. </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$bar </font><font color="#007700">= new </font><font color="#0000BB">Bar</font><font color="#007700">();<br />print </font><font color="#0000BB">$bar</font><font color="#007700">-></font><font color="#0000BB">fooStatic</font><font color="#007700">() . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5843" ></A ><P ><B >例 19-16. Static method example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">Foo </font><font color="#007700">{<br /> </font><font color="#0000BB">public </font><font color="#007700">static function </font><font color="#0000BB">aStaticMethod</font><font color="#007700">() {<br /> </font><font color="#FF8000">// ...<br /> </font><font color="#007700">}<br />}<br /><br /></font><font color="#0000BB">Foo</font><font color="#007700">::</font><font color="#0000BB">aStaticMethod</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></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.paamayim-nekudotayim.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.constants.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" >Class Constants</TD ></TR ></TABLE ></DIV ></BODY ></HTML >