Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After thinking about it, it's not really a bug. But it's definitely unintuitive.</p> <pre><code>&lt;?php class A { public function foo() { static::bar(); } public function bar() { echo "bar()\n"; } } $a = new A(); $a-&gt;foo(); </code></pre> <p>This is valid, and calls bar(). It does not mean call bar statically. It means to look up the current class name and call function <code>bar</code> if it exists, whether or not it's a static function.</p> <p>To clarify a bit more: do you think <code>parent::bar()</code> means to call a static function called <code>bar()</code>? No, it means call whatever function is named <code>bar()</code>. </p> <p>Consider:</p> <pre><code>&lt;?php class A { function __call($name, $args) { echo "__call()\n"; } static function __callStatic($name, $ags) { echo "__callStatic()\n"; } function regularMethod() { echo "regularMethod()\n"; } static function staticMethod() { echo "staticMethod()\n"; } } class B extends A { function foo() { parent::nonExistant(); static::nonExistant(); parent::regularMethod(); parent::staticMethod(); } } $b = new B(); $b-&gt;foo(); </code></pre> <p>The <code>parent::nonExistant()</code> method invokes <code>A::__call()</code>, as does <code>static::nonExistant()</code>. Invoking <code>A::__callStatic()</code> for <i>either</i> call would be equally as valid! There is no way for PHP to know which one you want to be called. However, by design, PHP gives <code>__call</code> the priority when invoked from this sort of context.</p> <p><code>parent::regularMethod()</code> invokes the non static function <code>regularMethod()</code>. (Again, the <code>::</code> operator does not mean "call this static function.") Likewise <code>parent::staticMethod()</code> invokes <code>A::staticMethod()</code> as one might expect.</p> <p>To solve your problem: </p> <p>You could manually call <code>self::__callStatic('foo')</code> in the inherited class.</p> <p>Or within the <code>__call</code> method of class A filter against a known list and call <code>self::__callStatic</code>.</p> <pre><code>function __call($f, $a) { if ($f == 'foo') return self::__callStatic($f, $a); else { } } </code></pre> <p>It's ugly, but at least people who extend the class won't need to do anything special.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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