Possible Duplicate:
php function overloading
I want to redeclare function such like this:
class Name{
function a(){ something; }
function a($param1){ something; }
}
but it returns
Fatal error: Cannot redeclare Name::a()
In java it just works. How can I do this in PHP?
Use default parameters:
class Name{
function a($param1=null){ something; }
}
If no parameter is passed to Name::a() it will assign a $param1 has a value of null. So basically passing that parameter becomes optional. If you need to know if it has a value or not you can do a simple check:
if (!is_null($param1))
{
//do something
}
You won't redeclare a function. Instead you can make an argument optional by assigning a default value to it. Like this:
function a($param1 = null){ something; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With