Tuesday, 15 May 2018

php - __callStatic(): instantiating objects from static context?




I am confused about how "static" and "dynamic" functions and objects in PHP work together especially with regards to __callStatic().




How __callStatic() works:



You can have a normal class MyClass, where within the class you can
put a static function called __callStatic(), which gets called only
when MyClass doesn't have a static function by the name you want.




i.e. I call MyClass::newFunction();



newFunction() is called statically but MyClass does not
have it declared. So, then __callStatic() gets called and
inside you can say



$myObject=new SomeOtherClass();
$myObject->newFunction();



which calls the function you wanted but on some other object.




Short Version:



In other words, __callStatic() does this:



MyClass::newFunction();



which is hiding this:



(new SomeOtherClass())->newFunction();


Say what now? What looks like code calling a static function from a class, turns out to be calling that function from some other class and calling it via instantiation, and not statically.



Explain this, please!



Why was it done? Can you do anything like this elsewhere, like C++ or Java? I am looking for short & concise, but informative explanation on static and dynamic functions in languages, and in this case whether __callStatic() violates or conforms to the big picture of Language constructs. Or is it a new language construct entirely.



Answer



__callStatic() provides developers with possibility to react on static method calls even if that methods don't exist or aren't accessible from outside of the class ( being protected). This is useful for dynamic, generic code generation.






Example: You have this class:



class Test {

protected static function myProtected($test) {

var_dump(__METHOD__, $test);
}

public static function __callStatic($method, $args) {
switch($method) {
case 'foo' :
echo 'You have called foo()';
var_dump($args);
break;


case 'helloWorld':
echo 'Hello ' . $args[0];
break;

case 'myProtected':
return call_user_func_array(
array(get_called_class(), 'myProtected'),
$args
);
break;

}

}

}


Try to call:



// these ones does not *really* exist

Test::foo('bar');
Test::helloWorld('hek2mgl');

// this one wouldn't be accessible
Test::myProtected('foo');

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...