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 6891765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:27:04+00:00 2026-05-27T06:27:04+00:00

tl;dr: Is there a way to prevent alteration to ( essentially lock ) variables

  • 0

tl;dr: Is there a way to prevent alteration to (essentially lock) variables declared/defined prior to an include() call, by the file being included? Also, somewhat related question.


I’m wondering about what measures can be taken, to avoid variable pollution from included files. For example, given this fancy little function:

/**
 * Recursively loads values by include returns into
 * arguments of a callback
 * 
 * If $path is a file, only that file will be included.
 * If $path is a directory, all files in that directory
 * and all sub-directories will be included.
 * 
 * When a file is included, $callback is invoked passing
 * the returned value as an argument.
 *
 * @param string $path
 * @param callable $callback
 */
function load_values_recursive($path, $callback){
    $paths[] = path($path);
    while(!empty($paths)){
        $path = array_pop($paths);
        if(is_file($path)){
            if(true === $callback(include($path))){
                break;
            }
        }
        if(is_dir($path)){
            foreach(glob($path . '*') as $path){
                $paths[] = path($path);
            }
        }
    }
}

I know it’s missing some type-checking and other explanations, let’s ignore those.

Anyways, this function basically sifts through a bunch of “data” files that merely return values (typically configuration arrays, or routing tables, but whatever) and then invokes the passed callback so that the value can be filtered or sorted or used somehow. For instance:

$values = array();
load_values_recursive('path/to/dir/', function($value) use(&$values){
    $values[] = $value;
});

And path/to/dir/ may have several files that follow this template:

return array(
    // yay, data!
);

My problem comes when these “configuration” files (or whatever, trying to keep this portable and cross-functional) start to contain even rudimentary logic. There’s always the possibility of polluting the variables local to the function. For instance, a configuration file, that for the sake of cleverness does:

return array(
    'path_1' => $path = 'some/long/complicated/path/',
    'path_2' => $path . 'foo/',
    'path_3' => $path . 'bar/',
);

Now, given $path happens to be a visible directory relative to the current, the function is gonna go wonky:

// ...
if(is_file($path)){
    if(true === $callback(include($path))){ // path gets reset to 
        break;                              // some/long/complicated/path/
    }
}
if(is_dir($path)){                          // and gets added into the
    foreach(glob($path . '*') as $path){    // search tree
        $paths[] = path($path);
    }
}
// ...

This would likely have bad-at-best results. The only1 solution I can think of, is wrapping the include() call in yet another anonymous function to change scope:

// ...
if(true === call_user_func(function() use($callback, $path){
    return $callback($path);
})){
    break;
}
// ...

Thus protecting $path (and more importantly, $callback) from causing side effects with each iteration.

I’m wondering if there exists a simpler way to “lock” variables in PHP under such circumstances.

  1. I just wanna go on the record here; I know I could use, for instance, an elseif to alleviate one of the issues specific to this function, however my question is more interested in circumstance-agnostic solutions, a catch-all if you will.
  • 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-27T06:27:05+00:00Added an answer on May 27, 2026 at 6:27 am

    I’ve gone with the following solution to include pollution:

    $value = call_user_func(function(){
        return include(func_get_arg(0));
    }, $path);
    

    $path is nowhere to be seen at inclusion, and it seems most elegant. Surely, calling func_get_arg($i) from the included file will yield passed values, but, well…

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

Sidebar

Related Questions

Is there a way to prevent a specific file from being opcode cached with
Is there a way to prevent a NSDrawer from being resized? I've tried setting
Is there a way to prevent empty elements of the form <myElement/> being used
Is there a way to prevent a user viewing an file but still use
HI, Is there a way to prevent a particular dll in C# being opened
is there a way to prevent triggering of property getter code while watching variables
Is there a way to prevent VS2008 creating browse info file files for C++
Is there a way to prevent a command from being added to the bash
Is there a way to prevent a page from being cached based on some
Is there a way to prevent users from getting into SQL Server Management Studio

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.