3

How would I include two functions with the same name in a php file?

I have two files:

file1.php which includes

function getResidentCount() {}

function getResidentData() {}

file2.php also includes

function getResidentCount() {}

function getResidentData() {}

I need file3.php to include all of these functions since each one is used to calculate different resident counts and data.

Any idea how I can include both of these files in one file. I cannot change the original files (they are in production for a live app).

Thanks, Claudia

4
  • 1
    Which PHP version do you use? Commented Oct 13, 2011 at 18:48
  • put them in a separate file and include that Commented Oct 13, 2011 at 18:48
  • A quick workaround could be using namespaces. Commented Oct 13, 2011 at 19:00
  • that would need the include files to be modified though? Commented Oct 13, 2011 at 19:03

5 Answers 5

4

This is evil. I only offer it as an exercise.

include('file1.php');
//include('file2.php'); 
$php=file_get_contents("file2.php");
$php=str_replace('getResidentCount', 'file2GetResidentCount', $php);
$php=str_replace('getResidentData', 'file2GetResidentData', $php);
eval($php);

Now you can call the duplicated function with a different name!

Do not do this. It's just an idea :)

An alternative would be use the APD extension, which lets you rename functions. Include the first file, rename its functions with rename_function, then include the second file.

Really, you should probably take a deep breath, delay what you're doing, and refactor what you have. Learn from it and change your practices so that it can't happen again. Namespaces might help you, a more object oriented approach might also.

Sign up to request clarification or add additional context in comments.

4 Comments

Nice solutions, regarding the dirtyness of the problem itself. :)
When I call the 'file2GetResidentCount', I get a "Fatal error: Call to undefined function SignificantgetResidentCount() ".
that's because it's a fairly naive search and replace - I'm guessing SignificantgetResidentCount is in different include file, but file2 will be trying to call Significantfile2GetResidentCount - refine the replacement, e.g. replace 'function getResidentCount' with 'function file2GetResidentCount'. Rinse, and repeat until it works. I feel bad just clarifying this. It's almost like you're going to use it...
Is it possible to require one file, then undefine the functions once you're done using them. Then require the second file?
2

It's not possible to include both files as is. Doing so would result in PHP Fatal error: Cannot redeclare ....

Paul Dixon has provided a work around if you can't otherwise clean up your code.

Comments

1

If you only need to use one of the functions per run of the script then you can use conditional inclusion:

if (some_condition) {
    require_once 'file1.php';
} else {
    require_once 'file2.php';
}

Comments

1

Looking for a solution to a similar answer, I was surprised to see nobody mentioned the anonymous function.

file1.php

$myfunction=function($val){
    //do something with val
    );

file2.php

$myfunction=function($val){
    //do something with val
    );

file3.php

for($i=1;$i<3;++$i){
    include_once('file'.$i.'.php');
    $result[$i]=$myfunction($val);
    }

The only drawback in this solution is that $myfunction gets overwritten, so it's not re-usable.

But this could perhaps be solved (not tested) with clone:

$allfunctions=[];    
for($i=1;$i<3;++$i){
        include_once('file'.$i.'.php');
        $allfunctions[$i]=clone $myfunction();
        }

Comments

0

I ended up nesting my functions for the page where I needed both sets of these functions. Thanks to all for your help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.