Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__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.

like image 801
Dennis Avatar asked Nov 07 '13 16:11

Dennis


People also ask

Can static methods instantiate?

What's a static method? In the same way that a static variable is associated with the class as a whole, so is a static method. In the same way that a static variable exists before an object of the class is instantiated, a static method can be called before instantiating an object.

What is __ call () in PHP?

When you call a method on an object of the Str class and that method doesn't exist e.g., length() , PHP will invoke the __call() method. The __call() method will raise a BadMethodCallException if the method is not supported. Otherwise, it'll add the string to the argument list before calling the corresponding function.

Whats static methods?

A static method (or static function) is a method defined as a member of an object but is accessible directly from an API object's constructor, rather than from an object instance created via the constructor.

Can we call static method in static method?

Characteristics of Static MethodsA static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.


1 Answers

__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');
like image 116
hek2mgl Avatar answered Oct 08 '22 06:10

hek2mgl