Page 1 of 1

calling two functions with the same name

Posted: Tue Aug 12, 2008 11:00 pm
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.

--

Posted: Tue Aug 12, 2008 11:06 pm
by earthpiper
--

Re: calling two functions with the same name

Posted: Tue Aug 12, 2008 11:16 pm
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.

Re: calling two functions with the same name

Posted: Wed Aug 13, 2008 2:58 pm
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().

Re: calling two functions with the same name

Posted: Wed Aug 13, 2008 4:32 pm
by pkbruker
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

Posted: Wed Aug 13, 2008 4:50 pm
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");
 
 

Re: calling two functions with the same name

Posted: Wed Aug 13, 2008 7:22 pm
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...