__call, __callStatic and calling scope in PHP -


i read calling scope , scope resolution operator (::) in php. there 2 variations: instance calling , statical calling. consider folowing listeng:

<?php  class {     public function __call($method, $parameters) {         echo "i'm __call() magic method".php_eol;     }      public static function __callstatic($method, $parameters) {         echo "i'm __callstatic() magic method".php_eol;     } }  class b extends {     public function bar() {         a::foo();     } }  class c {     public function bar() {         a::foo();     } }  a::foo(); (new a)->foo();  b::bar(); (new b)->bar();  c::bar(); (new c)->bar(); 

the result of execution (php 5.4.9-4ubuntu2.2) is:

i'm __callstatic() magic method i'm __call() magic method i'm __callstatic() magic method i'm __call() magic method i'm __callstatic() magic method i'm __callstatic() magic method 

i don't understand why (new c)->bar(); execute __callstatic() of a? instance calling should made in context of bar() method, isn't it? feature of php?

addition1:

moreover, if don't use magic methods , explicitly call, works expected:

<?php  class {     public function foo() {         echo "i'm foo() method of class".php_eol;         echo 'current class of $this '.get_class($this).php_eol;         echo 'called class '.get_called_class().php_eol;     } }  class b {     public function bar() {         a::foo();     } }  (new b)->bar(); 

result this:

i'm foo() method of class current class of $this b called class b 

in bar() method in c, have a::foo();:

public function bar() {     a::foo(); } 

as method neither creating instance of a, nor c extend a, :: operator being treated static-operator attempting call static method a::foo(). because foo() isn't defined on a, it's falling-back __callstatic() method.

if want call non-static method without extending a, you'll have create instance of a:

class c {     public function bar() {         $ainstance = new a();         $ainstance->foo();     } } 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -