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

  • Home
  • SEARCH
  • 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 3213698
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:58:28+00:00 2026-05-17T14:58:28+00:00

So the senario is that I want to have a custom function for requiring

  • 0

So the senario is that I want to have a custom function for requiring libraries. Something like:

define('E_ROOT', str_replace('//','/',dirname(__FILE__)));
/* ... */
function e_load($fn, $allowReloading = FALSE) {
    $inc = E_ROOT.'/path/here/'.$fn.'.php';
    if($allowReloading)
        require $inc; // !!!
    else
        require_once $inc; // !!!
}

The problem being that require and require_once will load the files into the namespace of the function, which doesn’t help for libraries of functions, classes, et cetera. So is there a way to do this?

(Something avoiding require and require_once altogether is fine, as long as it doesn’t use eval since it’s banned on so many hosts.)

Thanks!

  • 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-05-17T14:58:29+00:00Added an answer on May 17, 2026 at 2:58 pm

    Technically include() is meant to act as though you’re inserting the text of included script at that point in your PHP. Thus:

    includeMe.php:
    <?php
        $test = "Hello, World!";
    ?>
    
    includeIt.php:
    <?php
        include('includeMe.php');
        echo $test;
    ?>
    

    Should be the exact same as:

    <?php
        /* INSERTED FROM includeMe.php */
        $test = "Hello, World!";
        /* END INSERTED PORTION */
        echo $test;
    ?>
    

    Realizing this, the idea of making a function for dynamically including files makes about as much sense (and is about as easy to do) as having dynamic code all-together. It’s possible, but it will involve a lot of meta-variables.

    I’d look into Variable Variables in PHP as well as the get_defined_vars function for bringing variables into the global scope. This could be done with something like:

    <?php
    define('E_ROOT', str_replace('//','/',dirname(__FILE__)));
    /* ... */
    function e_load($fn, $allowReloading = FALSE) {
    
        $prev_defined_vars = get_defined_vars();
    
        $inc = E_ROOT.'/path/here/'.$fn.'.php';
        if($allowReloading)
            require $inc; // !!!
        else
            require_once $inc; // !!!
    
        $now_defined_vars = get_defined_vars();
    
        $new_vars = array_diff($now_defined_vars, $prev_defined_vars);
    
        for($i = 0; $i < count($new_vars); $i++){
            // Pull new variables into the global scope
            global $$newvars[$i];
        }
    }
    ?>
    

    It may be more convenient to just use require() and require_once() in place of e_load()

    Note that functions and constants should always be in the global scope, so no matter where they are defined they should be callable from anywhere in your code.

    The one exception to this is functions defined within a class. These are only callable within the namespace of the class.

    EDIT:

    I just tested this myself. Functions are declared in the global scope. I ran the following code:

    <?php
    function test(){
        function test2(){
            echo "Test2 was called!";
        }
    }
    
    //test2(); <-- failed
    test();
    test2(); // <-- succeeded this time
    ?>
    

    So the function was only defined after test() had been run, but the function was then callable from outside of test(). Therefore the only thing you should need to pull into the global scope are your variables, via the script I provided earlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the scenario: I have a set of buttons that I want to bind
I have this senario. We have an application server that contains a few web
I want to use custom delegate in my search tab. I have never used
I have to choose between custom data tags or ids. I would like to
Scenario: I have several services that I want to be discovered by different clients.
I have a custom class.And I want if any other class instantiate it then
The problem: I have a custom collection PagedList<T> that is being returned from my
I'm want to parse a custom string format that is persisting an object graphs
I have defined a custom html attribute data-something-something. In my view I use an
I have a custom Android Service (with no associated Activity) that needs to implement

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.