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 butMyClass
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.
No comments:
Post a Comment