Function Overloading in PHP5 and PHP4
February 12th, 2005
This is a solution to the problem of argument type based overloading in PHP. As far as I know this should work in PHP4 and PHP5.
class Viewer {
function display($v) {
$class = get_class($v);
$function = "display_$class";
if(method_exists($this, $function) ){
$this->$function($v);
} else {
$this->display_default($v);
}
}
}
This class can then be extended and used like this:
class PDOViewer extends Viewer {
function display_PDO($item) {
echo "This is a PDO item";
}
}
$viewer = new PDOViewer;
$viewer->display(new PDO);
Function Overloading in PHP » Wakeless.net said:
[…] Update: I have written about a more Java-like, argument based overloading solution here. […]
Marl Atkins said:
I don’t see how you’re exhibiting an overload situation.
The second class never overloads the first.
The 2 functions aren’t named the same thing.
Overloading involves 2 functions with the same name having a different number of arguments in the same class.
You could put the second function into another class that extends this one and it would still be considered an ‘overload’.
I’m pretty sure you can’t do it in php.
[code]
class MyOverloader
{
function Overload($sArg1)
{
return $sArg1 . ” a simple function executed”;
}
function Overload($sArg1, $sArg2)
{
return $sArg1 . $sArg2;
}
}
[/code]
Florida Web Design ^ a Firm in Orlando
Aron said:
http://digg.com/celebrity/Lisa_Ann_2
Hero said: