Imagine I have two php scripts script.php
and inc.php
. (<?php
omitted)
inc.php:
$foo = 'a';
script.php:
$foo = 'b'; // $foo is b
include 'inc.php'; // $foo is a
Then in the moment that inc.php
is included the variable $foo
is overwritten with 'a'
. I'd like to have files that can be included without side effect.
The most practical would be a sort of local scope:
inc.php (2):
// $foo is b
{
$foo = 'a'; // $foo is a
} // $foo is b
By my knowledge no such construct exists, the only thing I could think of was something like:
inc.php (3):
// $foo is b
call_user_func(function() {
$foo = 'a'; // $foo is a
}); // $foo is b
- Are there other (better / more elegant) ways to make a file safe for inclusion?
- Is there a way I can safely include
inc.php
intoscript.php
ifinc.php
cannot be modified?