Page 1 of 1

Alternative to Function Overloading in PHP 5

Posted: Mon Sep 08, 2008 10:37 am
by ericm
Since I don't believe function overloading is availabe in PHP 5, is there a good alternate approach for having one function name capable of accepting different types/counts of parameters? Is there something better than checking the variable type within the function?

Code: Select all

 
function A(array) { // do something w/ the array }
function A(string) { // do something w/ the string }
 
A(array()); // calls the array version
A("string"); // calls the string version
 
 

Re: Alternative to Function Overloading in PHP 5

Posted: Mon Sep 08, 2008 11:21 am
by allspiritseve

Re: Alternative to Function Overloading in PHP 5

Posted: Mon Sep 08, 2008 12:05 pm
by Christopher
ericm wrote:Since I don't believe function overloading is availabe in PHP 5, is there a good alternate approach for having one function name capable of accepting different types/counts of parameters? Is there something better than checking the variable type within the function?
Unlike other languages that enforce parameter type checking, you do overloading in PHP by checking the variable type within the function. So it provides the same functionality in a different way. Which you consider 'better' probably depends on which you used first.