Just wanted to know if this is possible:
I have 2 files, a.php and b.php. Both have function hello($name) with different outputs.
The functions name is the same, but the contents are not...
Is it possible to somehow call these functions without redeclaration error? Anybody know of a work-around?
Thanks.
calling two functions with the same name
Moderator: General Moderators
-
jonwondering
- Forum Commoner
- Posts: 39
- Joined: Mon Mar 13, 2006 6:26 pm
-
earthpiper
- Forum Newbie
- Posts: 3
- Joined: Tue Aug 12, 2008 10:38 pm
-
jonwondering
- Forum Commoner
- Posts: 39
- Joined: Mon Mar 13, 2006 6:26 pm
Re: calling two functions with the same name
i am not using them yet. because i don't know how to. i've tried to use include, and then call the hello function but that didn't work.
it would be nice if php had some sort of exclude function, that way i could include files in a loop, call hello function, and then exclude the file and include the next one and repeat.
it would be nice if php had some sort of exclude function, that way i could include files in a loop, call hello function, and then exclude the file and include the next one and repeat.
Re: calling two functions with the same name
Both include() and require() essentially just copy the files into a script where they are called, so function names must be unique. Now if you want to use OOP, you can create classes that have methods with the same names, but not just with include() / require().
Re: calling two functions with the same name
Functions with same name (except classes) does not work, and is bad programming. Create classes, and problem solved.
Re: calling two functions with the same name
Why not use a second parameter? For example:
Code: Select all
function hello($name, $version="EN") {
switch ($version) {
case "EN":
return "Hello ".$name;
break;
case "FR":
return "Bienvenue ".$name;
break;
case "DE":
return "Guten tag ".$name;
break;
case "IT":
return "Ciao ".$name;
break;
default:
return "Welcome ".$name;
break;
}
}
echo hello("Chris","EN");
echo hello("Sarah","FR");
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: calling two functions with the same name
You should be asking yourself why you have two methods of the same name...then why you need to include them both at the same time...