Alternative to Function Overloading in PHP 5

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ericm
Forum Newbie
Posts: 17
Joined: Tue Aug 26, 2008 8:33 am

Alternative to Function Overloading in PHP 5

Post 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
 
 
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Alternative to Function Overloading in PHP 5

Post by allspiritseve »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Alternative to Function Overloading in PHP 5

Post 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.
(#10850)
Post Reply