Created
June 3, 2016 12:19
-
-
Save pssubashps/4b858e5173a9dbc5557f06b018118abd to your computer and use it in GitHub Desktop.
PHP-method overloading-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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