Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:06:56+00:00 2026-06-07T14:06:56+00:00

Is it possible to define the same function in two files, include them and

  • 0

Is it possible to define the same function in two files, include them and then call both function in one go, e.g.

== File1.php ==

function my_function() {
echo "File 1!";
}

== File2.php ==

function my_function() {
echo "File 2!";
}

== File3.php ==

include('File1.php');
include('File2.php');
my_function();

–
Which would output:

File 1!
File 2!

Is this is not possible how would I work around that?

Thanks,
Thomas Edwards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:06:57+00:00Added an answer on June 7, 2026 at 2:06 pm

    You can’t redefine global functions (outside of runkit, but that’s bad voodoo) so your options remain of using a class driven solution, or an anonymous function driven solution as follows.

    You could write a function to assemble all included functions into a call queue (which is simply another anonymous function that runs the queue):

    function queueFunctions(array $functions){
        return function() 
        use ($functions) {
            foreach($functions as $function) {
                $function();
            }
        };
    }
    

    And return anonymous functions from each of the files as you described, rather than redefine a global function:

    file1.php

    return function() {
        echo 'Hello';
    };
    

    file2.php

    return function() {
        echo 'World';
    };
    

    Then, you can:

    // both
    $function = queueFunctions([
        include('file1.php'), 
        include('file2.php'),
    ]);
    
    $function(); // HelloWorld
    
    // one of them    
    $function = queueFunctions([
        include('file2.php'),
    ]);
    
    $function(); // World
    
    // repeated calls
    $function = queueFunctions([
        include('file2.php'),
        include('file1.php'),
        include('file2.php'),
    ]);
    
    $function(); // WorldHelloWorld
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to define a member function which have the return type same
Possible Duplicate: Is it possible to define more than one function per file in
Is it possible to define some sort of capitalize() or toLowerCase() function when creating
Possible Duplicate: PHP method chaining? I had a function for create user, like cruser
I have two files: // event_test_delete.cpp #include <event.h> int main() { event_base* ev; ev
Is it possible to define a function in Google Spreadsheets that can be used
Is that possible to define a function without referencing to self this way? def
Possible Duplicate: Undefined, unspecified and implementation-defined behavior I know calling delete on same object
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.