<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Static Keyword</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.abstract.html">Class Abstraction</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.oop5.static" class="sect1"> <h2 class="title">Static Keyword</h2> <div class="tip"><strong class="tip">Tip</strong> <p class="simpara"> This page describes the use of the <em>static</em> keyword to define static methods and properties. <em>static</em> can also be used to <a href="language.variables.scope.html#language.variables.scope.static" class="link">define static variables</a> and for <a href="language.oop5.late-static-bindings.html" class="link">late static bindings</a>. Please refer to those pages for information on those meanings of <em>static</em>. </p> </div> <p class="para"> Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can). </p> <p class="para"> For compatibility with PHP 4, if no <a href="language.oop5.visibility.html" class="link">visibility</a> declaration is used, then the property or method will be treated as if it was declared as <em>public</em>. </p> <p class="para"> Because static methods are callable without an instance of the object created, the pseudo-variable <var class="varname"><var class="varname">$this</var></var> is not available inside the method declared as static. </p> <p class="para"> Static properties cannot be accessed through the object using the arrow operator ->. </p> <p class="para"> Calling non-static methods statically generates an <strong><code>E_STRICT</code></strong> level warning. </p> <p class="para"> Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object. </p> <p class="para"> As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. <em>self</em>, <em>parent</em> and <em>static</em>). </p> <div class="example" id="example-194"> <p><strong>Example #1 Static property example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br /> public static </span><span style="color: #0000BB">$my_static </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /><br /> public function </span><span style="color: #0000BB">staticValue</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$my_static</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">Bar </span><span style="color: #007700">extends </span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">fooStatic</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">$my_static</span><span style="color: #007700">;<br /> }<br />}<br /><br /><br />print </span><span style="color: #0000BB">Foo</span><span style="color: #007700">::</span><span style="color: #0000BB">$my_static </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br />print </span><span style="color: #0000BB">$foo</span><span style="color: #007700">-></span><span style="color: #0000BB">staticValue</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />print </span><span style="color: #0000BB">$foo</span><span style="color: #007700">-></span><span style="color: #0000BB">my_static </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// Undefined "Property" my_static <br /><br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$foo</span><span style="color: #007700">::</span><span style="color: #0000BB">$my_static </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$classname </span><span style="color: #007700">= </span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br />print </span><span style="color: #0000BB">$classname</span><span style="color: #007700">::</span><span style="color: #0000BB">$my_static </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; </span><span style="color: #FF8000">// As of PHP 5.3.0<br /><br /></span><span style="color: #007700">print </span><span style="color: #0000BB">Bar</span><span style="color: #007700">::</span><span style="color: #0000BB">$my_static </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Bar</span><span style="color: #007700">();<br />print </span><span style="color: #0000BB">$bar</span><span style="color: #007700">-></span><span style="color: #0000BB">fooStatic</span><span style="color: #007700">() . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-195"> <p><strong>Example #2 Static method example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Foo </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">aStaticMethod</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">// ...<br /> </span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">Foo</span><span style="color: #007700">::</span><span style="color: #0000BB">aStaticMethod</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$classname </span><span style="color: #007700">= </span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$classname</span><span style="color: #007700">::</span><span style="color: #0000BB">aStaticMethod</span><span style="color: #007700">(); </span><span style="color: #FF8000">// As of PHP 5.3.0<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.abstract.html">Class Abstraction</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>