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
Moderator: General Moderators
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
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.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?