PHP5: Object function with multiple argument types
Posted: Sat Oct 23, 2004 1:59 am
With objects in php5 you can now require that an object-argument passed to a function be an instance of a specific class.
If $f is not an instance of Foo, something like this error occurs:
"Fatal error: Argument 1 must be an instance of foo"
But, is it possible to have multiple copies of the same function, but accepting different argument-types?
I want to do different commands based on what type of object is passed, and I don't want to create different function names. I suppose I could just create if/else instanceof checks, but would something like the above be possible?
Thanks
Code: Select all
function doStuff(Foo $f)
{
echo $f->printStuff();
}"Fatal error: Argument 1 must be an instance of foo"
But, is it possible to have multiple copies of the same function, but accepting different argument-types?
Code: Select all
function doStuff(Foo $f)
{
echo $f->printStuff();
}
function doStuff(Bar $f)
{
echo $f->deleteStuff();
}
function doStuff(Moo $f)
{
echo $f->printOtherStuff();
}Thanks