<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.oop5.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.oop5.static.php',
    1 => 'Static Keyword',
    2 => 'Static Keyword',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Classes and Objects',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.paamayim-nekudotayim.php',
    1 => 'Scope Resolution Operator (::)',
  ),
  'next' => 
  array (
    0 => 'language.oop5.abstract.php',
    1 => 'Class Abstraction',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/oop5/static.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<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 <code class="literal">static</code> keyword to
    define static methods and properties. <code class="literal">static</code> can also
    be used to
    <a href="language.variables.scope.php#language.variables.scope.static" class="link">define static variables</a>,
    <a href="functions.anonymous.php#functions.anonymous-functions.static" class="link">define static anonymous functions</a>
    and for
    <a href="language.oop5.late-static-bindings.php" class="link">late static bindings</a>.
    Please refer to those pages for information on those meanings of
    <code class="literal">static</code>.
   </p>
  </div>

  <p class="para">
   Declaring class properties or methods as static makes them accessible
   without needing an instantiation of the class.
   These can also be accessed statically within an instantiated class object.
  </p>

  <div class="sect2" id="language.oop5.static.methods">
   <h3 class="title">Static methods</h3>

   <p class="para">
    Because static methods are callable without an instance of
    the object created, the pseudo-variable <var class="varname">$this</var> is
    not available inside methods declared as static.
   </p>

   <div class="warning"><strong class="warning">Warning</strong>
    <p class="para">
     Calling non-static methods statically throws an <span class="classname"><a href="class.error.php" class="classname">Error</a></span>.
    </p>
    <p class="para">
     Prior to PHP 8.0.0, calling non-static methods statically was deprecated, and
     generated an <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong> warning.
    </p>
   </div>

   <div class="example" id="example-1">
    <p><strong>Example #1 Static method example</strong></p>
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

   </div>
  </div>
  
  <div class="sect2" id="language.oop5.static.properties">
   <h3 class="title">Static properties</h3>

   <p class="para">
    Static properties are accessed using the
    <a href="language.oop5.paamayim-nekudotayim.php" class="link">Scope Resolution Operator</a>
    (<code class="literal">::</code>) and cannot be accessed through the object operator
    (<code class="literal">-&gt;</code>).
   </p>

   <p class="para">
    It&#039;s possible to reference the class using a variable.
    The variable&#039;s value cannot be a keyword (e.g. <code class="literal">self</code>,
    <code class="literal">parent</code> and <code class="literal">static</code>).
   </p>

   <div class="example" id="example-2">
    <p><strong>Example #2 Static property example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">-&gt;</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">-&gt;</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">;<br /><br />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">-&gt;</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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Output of the above example in PHP 8 is similar to:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
foo
foo

Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23

Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23

foo
foo
foo
foo
</pre></div>
    </div>
   </div>
  </div>
 </div><?php manual_footer($setup); ?>