I am having trouble with naming the function. I have a class and I need 2 functions something like below.
class myclass {
public function tempclass () {
echo "default";
}
public function tempclass ( $text ) {
echo $text;
}
}
When I call
tempclass('testing'); // ( called after creating the object )
function tempclass() is being called how can i have 2 functions with same name but different parameters?
Traditional overloading is not currently possible in PHP. Instead you’ll need to check the arguments passed, and determine how you’d like to respond.
Check out
func_num_argsandfunc_get_argsat this point. You could use both of these internally to determine how you should respond to the invocation of certain methods. For instance, in your case, you could do the following:Alternatively, you could provide default values for your arguments as well, and use those to determine how you ought to react: