Answer the question
In order to leave comments, you need to log in
What is the name of such a paradigm/pattern (description inside)?
class SomeClass {
__call($functionName, $arguments)
{
return call_user_func([ self, $functionName . '_' ], $arguments);
}
__callStatic($functionName, $arguments)
{
return call_user_func([ self, $functionName . '_' ], $arguments);
}
private function someFunction_(){
if(empty($this))
{
echo 'статичный контекст';
}
else
{
echo 'объектный контекст';
}
}
}
$obj = new SomeClass;
$obj->someFunction(); // 'объектный контекст';
SomeClass::someFunction(); // 'статичный контекст'
Answer the question
In order to leave comments, you need to log in
I think this pattern is called a "crutch".
Besides, E_STRICT "non-static method SomeClass::someFunction_() should not be called statically" is still there.
class SomeClass {
public function someFunction(){
if(empty($this))
{
echo 'статичный контекст';
}
else
{
echo 'объектный контекст';
}
}
}
$obj = new SomeClass;
$obj->someFunction(); // 'объектный контекст';
SomeClass::someFunction(); // 'статичный контекст'
It's hard to even say what to call this trick...
In fact, it's intercepting a method call and determining the context of this very call
. I don't think it makes sense to come up with a name for such things. In the general case, this is just a part of some pattern (maybe a fluent interface). Try to scroll through the article and read the comments here: habrahabr.ru/post/175935
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question