__callStatic (): creating objects from a static context?

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

How __callStatic () works:

You can have a regular MyClass class, where inside the class you can have a static function called __callStatic (), which is called only when MyClass does not have a static function by the name you want.

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

newFunction() is called static, but MyClass did not declare it. So then __callStatic() is called and inside you can say

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

which calls the function you want, but on some other object.

Short version:

In other words, __callStatic () does the following:

 MyClass::newFunction(); 

which hides this:

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

Say what now? What looks like code that calls a static function from a class turns out to be calling this function from some other class and calling it through an instance, not statically.

Explain it please!

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

+6
source share
3 answers

__callStatic() provides developers with the ability to respond to calls to static methods, even if these methods do not exist or are not accessible from outside the class ( protected ). This is useful for generating dynamic generic code. If you don’t see how useful this is, you may not need this feature at the moment and should continue to learn the basics.


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 calling:

 // these ones does not *really* exist Test::foo('bar'); Test::helloWorld('hek2mgl'); // this one wouldn't be accessible Test::myProtected('foo'); 

Got it

+6
source

I'm not quite sure why __callStatic () is important here?

I do not quite understand the difference between:

 class Foo { static function bar() { $obj = new Foo(); $obj->quux(); } function quux() { return "whatever"; } } 

and your example? In both scenarios, you invoke a static method that calls another method on an object of the same class.

Yes it is possible. Actually, this suggests that you might want to reorganize your code. In the example, you create an object with its default state, execute a method on it, and then drop the object. This indicates that the method that does not actually need the state of the objects. This means that it either does not belong to this class, or simply can be rewritten as a static method.

And do you know about __call? It does the same as __callStatic, but for objects, not classes. For instance. $ Foo-> myMissingMethod (); will go to __call if such a method exists for a class for which $ foo is an instance.

+1
source

Why was this done?

This is an existential question, but I think the answer is "because you can." PHP is a dynamic language, and these __call and __callStatic() constructs show their power, and this power can be useful in some situations.

Can you do something like this elsewhere, like C ++ or Java?

No. They have no such magical methods.

I am looking for a brief and concise but informative explanation of static and dynamic functions in languages

Static functions encapsulate code. They exist even if no class has been created. You can call them using the region resolution operator. Dynamic functions well dynamic

... __callStatic () violates or matches the big picture of language constructs. Or is this the whole new language construct.

This is a dynamic language construct. Not sure if this functionality exists in all dynamic languages, but it works in PHP. This does not violate anything, it simply introduces a new paradigm of end-to-end catch-all functions when the function you are calling does not exist / is not available in the current area.

+1
source

Source: https://habr.com/ru/post/957605/


All Articles