Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I repeated the test on my machine multiple times and surprisingly <strong>you are right!</strong></p> <p><strong><a href="http://vanylla.it/tests/static-method-vs-object.php">In PHP calling methods of <code>static</code> class seems to be slower than calling object methods. Click here for simple test.</a></strong></p> <p>The code with the running test is in the above link.</p> <p><strong>I even tried placing both the objet method and the static method in the same class and the <code>static</code> method still results SLOWER!!!</strong></p> <p>At this point I'm wondering how slow could be a call to a <code>static</code> method of an inherited class, since inheritance adds up delay.</p> <p>Sadly, I'm clueless about the reason. <em>Maybe PHP takes more time in finding the definition of the <code>static</code> method</em>.</p> <p>As a side note I could only say that in a real life application it usually happens to have the object created before calling one of its methods. Therefor your test should take this into account comparing the loop of static calls to a loop that each time (or at least <em>some times</em>) [*] creates the objet:</p> <pre><code>for($i=0; $i&lt;10*1000*1000; $i++) { $someObj = new someObj(); $someObj-&gt;doTest($i); } </code></pre> <p>thus is obviously slower than the <code>static</code> call.</p> <pre><code>for($i=0; $i&lt;10*1000*1000; $i++) { SomeClass::doTest($i); } </code></pre> <hr> <p>[*] the problem is: how much is <em>some times</em> in order to simulate what happnes in a real world app? It's hard to say!</p>
 

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