Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's assume this is your class:</p> <pre><code>class Test { private $baz = 1; public function foo() { ... } public function bar() { printf("baz = %d\n", $this-&gt;baz); } public static function staticMethod() { echo "static method\n"; } } </code></pre> <p>From within the <code>foo()</code> method, let's look at the different options:</p> <pre><code>$this-&gt;staticMethod(); </code></pre> <p>So that calls <code>staticMethod()</code> as an instance method, right? It does not. This is because the method is declared as <code>public static</code> the interpreter will call it as a static method, so it will work as expected. It could be argued that doing so makes it less obvious from the code that a static method call is taking place.</p> <pre><code>$this::staticMethod(); </code></pre> <p>Since PHP 5.3 you can use <code>$var::method()</code> to mean <code>&lt;class-of-$var&gt;::</code>; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method:</p> <pre><code>self::staticMethod(); </code></pre> <p>Now, before you start thinking that the <code>::</code> is <em>the</em> static call operator, let me give you another example:</p> <pre><code>self::bar(); </code></pre> <p>This will print <code>baz = 1</code>, which means that <code>$this-&gt;bar()</code> and <code>self::bar()</code> do exactly the same thing; that's because <code>::</code> is just a scope resolution operator. It's there to make <code>parent::</code>, <code>self::</code> and <code>static::</code> work and give you access to static variables; how a method is called depends on its signature and how the caller was called.</p> <p>To see all of this in action, see <a href="http://3v4l.org/DnSUk" rel="noreferrer">this 3v4l.org output</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload