Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you try running the code below and look at the backtrace output you will see that PHP converts instanceFunc() to a static method when called in a static context. However, in an instance context it will treat it as a instance call. </p> <p>If you introduce instance variables in to the mix (remove commented out lines) then a fatal error is encountered when called instanceFunc() from a static call. </p> <p>This implies that PHP allows all methods that are static in nature (no instance variables are worked with) to be called from a static context, as soon as this contract is broken however an error will be produced. Therefore the use of static functions seems to be only good practice in keeping with other Object Orientated languages. </p> <p>With regards to staticFunc() both calls show that PHP interprets these as static calls, which is to be expected. </p> <pre><code>class A { private $x = 5; private $y = 6; private $z; public function instanceFunc() { //$this-&gt;z = $this-&gt;y * $this-&gt;x; //echo $this-&gt;z; var_dump(reset(debug_backtrace())); } public static function staticFunc() { var_dump(reset(debug_backtrace())); } } $a = new A(); A::instanceFunc(); // static call of intended instance method $a-&gt;instanceFunc(); // instance call of instance method A::staticFunc(); $a-&gt;staticFunc(); </code></pre> <p>Example output (code running with comments):</p> <pre><code>array(6) { ["file"]=&gt; string(59) "test.php" ["line"]=&gt; int(19) ["function"]=&gt; string(12) "instanceFunc" ["class"]=&gt; string(1) "A" ["type"]=&gt; string(2) "::" ["args"]=&gt; array(0) { } } array(7) { ["file"]=&gt; string(59) "test.php" ["line"]=&gt; int(22) ["function"]=&gt; string(12) "instanceFunc" ["class"]=&gt; string(1) "A" ["object"]=&gt; object(A)#8 (3) { ["x:private"]=&gt; int(5) ["y:private"]=&gt; int(6) ["z:private"]=&gt; NULL } ["type"]=&gt; string(2) "-&gt;" ["args"]=&gt; array(0) { } } array(6) { ["file"]=&gt; string(59) "test.php" ["line"]=&gt; int(24) ["function"]=&gt; string(10) "staticFunc" ["class"]=&gt; string(1) "A" ["type"]=&gt; string(2) "::" ["args"]=&gt; array(0) { } } array(6) { ["file"]=&gt; string(59) "test.php" ["line"]=&gt; int(26) ["function"]=&gt; string(10) "staticFunc" ["class"]=&gt; string(1) "A" ["type"]=&gt; string(2) "::" ["args"]=&gt; array(0) { } } </code></pre>
    singulars
    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.
    2. VO
      singulars
      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