Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pssubashps/4b858e5173a9dbc5557f06b018118abd to your computer and use it in GitHub Desktop.
Save pssubashps/4b858e5173a9dbc5557f06b018118abd to your computer and use it in GitHub Desktop.
PHP-method overloading-1
<?php
class MyClass {
public function __call($name, $args) {
switch ($name) {
case 'funcOne':
switch (count($args)) {
case 1:
return call_user_func_array(array($this, 'funcOneWithOneArg'), $args);
case 3:
return call_user_func_array(array($this, 'funcOneWithThreeArgs'), $args);
}
case 'anotherFunc':
switch (count($args)) {
case 0:
return $this->anotherFuncWithNoArgs();
case 5:
return call_user_func_array(array($this, 'anotherFuncWithMoreArgs'), $args);
}
}
}
protected function funcOneWithOneArg($a) {
echo __FUNCTION__;
}
protected function funcOneWithThreeArgs($a, $b, $c) {
echo __FUNCTION__;
}
protected function anotherFuncWithNoArgs() {
echo __FUNCTION__;
}
protected function anotherFuncWithMoreArgs($a, $b, $c, $d, $e) {
echo __FUNCTION__;
}
}
$m = new MyClass;
$m->funcOne(1,2,3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment