Function Overloading in PHP
April 19th, 2005
Update: I have written about a more Java-like, argument based overloading solution here.
I’ve been trying to get a set of good basic functions included within my PHP framework and ran across a nice little trick to allow function (without a class) overloading, the code should speak for itself.
This will be the default file.
if(!function_exists("overloadableFunc") ) {
function overloadableFunc() {
echo "foo";
}
}
and this will be in the overloading file (needs to be included before the default.
function overoadableFunc() {
echo "foobar";
}
Marl Atkins said:
Hmm, what if I want to use the overloaded call in the overloading class?
I can do this is C# and Java:
In this case I’m really doing both an overload AND an override.
That is, the second class uses the name of a function that is also included in the class that it extends, but the number of arguments are not the same.
I fear this is going to be a nightmare in php.
I’ve seen some code that uses args[] to kind of simulate it.
Any ideas?
[code]
class Overloaded
{
function OverLoadMe($sVal1, $sVal2)
{
return $sVal1 . $sVal2
}
}
class OverLoader extends Overloaded
{
$sVal1 = "I got this internally, maybe from a database";
function OverLoadMe($sVal2)
{
return this->OverLoadMe($sVal1, $sVal2);
}
}
[/code]
Florida Web Design, Inc. of Orlando
Esther Cruz said:
ue01hucnd3dkil35
Igor Cemim said:
Thanks. ^^