calling two functions with the same name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

calling two functions with the same name

Post by jonwondering »

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.
earthpiper
Forum Newbie
Posts: 3
Joined: Tue Aug 12, 2008 10:38 pm

--

Post by earthpiper »

--
Last edited by earthpiper on Sat Feb 12, 2011 5:07 am, edited 1 time in total.
jonwondering
Forum Commoner
Posts: 39
Joined: Mon Mar 13, 2006 6:26 pm

Re: calling two functions with the same name

Post by jonwondering »

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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: calling two functions with the same name

Post by califdon »

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().
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: calling two functions with the same name

Post by pkbruker »

Functions with same name (except classes) does not work, and is bad programming. Create classes, and problem solved.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: calling two functions with the same name

Post by onion2k »

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

Post by alex.barylski »

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...
Post Reply