Trying to achieve method overloading using PHP (don't confuse with overloading definition from PHP manual), is not easy there is clear trade off, because of nature of PHP you have to have one method and if
or switch
statement inside to provide overloading, it creates procedural code that is difficult to read in long methods.
Is there any advantage of having method overloading in PHP, can it be achieved in any other way?
class MyClass {
public function doMany($input) {
if (is_array($input)) {
...
} else if (is_float($input)) {
...
}
}
}
versus traditional approach
class MyClass {
public function doArray(array $input) {
...
}
public function doFloat(float $input) {
...
}
}