Skip to content

Instantly share code, notes, and snippets.

@oxechicao
Last active April 22, 2019 09:09
Show Gist options
  • Save oxechicao/7b755660c4e899934a7f2f8d80242cd6 to your computer and use it in GitHub Desktop.
Save oxechicao/7b755660c4e899934a7f2f8d80242cd6 to your computer and use it in GitHub Desktop.
Call a method with the same signature staticly and normally.
<?php
class Teste
{
public $var = 'Variável pública';
public $macroFunction = ['foo' => 'fooFunction'];
public $staticMacro = ['foo' => 'fooStatic'];
public function fooFunction()
{
echo $this->var;
}
public static function fooStatic()
{
return (new static)->fooFunction();
}
public static function __callStatic($name, $arguments)
{
echo 'static' . "\n";
return call_user_func(array('Teste', (new static)->staticMacro[$name]));
}
public function __call($name, $arguments)
{
echo 'normal' . "\n";
return call_user_func(array($this, $this->macroFunction[$name]), $arguments);
}
}
Teste::foo();
(new Teste)->foo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment