0

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
  1. Are there other (better / more elegant) ways to make a file safe for inclusion?
  2. Is there a way I can safely include inc.php into script.php if inc.php cannot be modified?
Samuel
  • 18,286
  • 18
  • 52
  • 88
  • why not [namespaces](http://www.php.net/manual/en/language.namespaces.basics.php) ? – JohnnyJS May 25 '14 at 16:15
  • 1
    the password should be in a configuration file and not within the source code. same for the DSN string and the username. you're most likely missing basic concepts. If you construct this scenario, please create a new example from scratch with as little code as necessary to show your issue (e.g. I can imagine this does not need two files, a 10 line script (if even) should be enough to make your point. – hakre May 25 '14 at 16:19
  • @hakre I adapted the example to focus on the issue at hand. – Samuel May 25 '14 at 16:40
  • @JohnnyJS I thought variables were unaffected by namespaces? – Samuel May 25 '14 at 16:45
  • 1
    @Samuel: Why multiple files? This does not need necessary for the minimum example. Please turn this into a more clean and easier to reproduce example(s). – hakre May 25 '14 at 16:48
  • you can return value within the required file. Like `$foo = require('somefile.php');` and in somefile.php `return 'wow';` – Ronni Skansing May 25 '14 at 17:17
  • @hakre The point of my question is about (1) how to make a file that can be included safely, and (2) how do I include a file without allowing it to override local variables in the file including it. – Samuel May 25 '14 at 22:52
  • @RonniSkansing I didn't know that, interesting, the required file will still have side affects though in terms of overriding local variables right? – Samuel May 25 '14 at 22:53
  • 1
    @Samuel: You might be looking for a function like in this answer: http://stackoverflow.com/a/7697490/367456 – hakre May 26 '14 at 09:04
  • @hakre That looks like it might address this problem :) – Samuel May 26 '14 at 14:44
  • @hakre Would you consider applying that answer to this question? Then I can accept it – Samuel Jul 06 '14 at 20:26
  • well, re-reading the question I find it hard to word an answer with that function quickly and I also don't like to copy that answer over verbatim. Are you okay that I close-vote against the other question as duplicate instead? That would mark it "resolved" and it also links to the existing answer. ok? – hakre Jul 06 '14 at 20:31
  • @hakre Sure, it just bothers me as an unanswered, down voted question where the right answer is hidden in the comment section! – Samuel Jul 06 '14 at 20:33
  • @Samuel: The close vote changes nothing about the voting (up/down) however, it's marked as solved in the sense that a new answer can't be given any longer. – hakre Jul 06 '14 at 21:01

1 Answers1

0

Set constants:

defined('PASSWORD') or define('PASSWORD', 'value');

Constants are global but cannot do defined 2 times. If you define PASSWORD constant in "db.php" then cannot to be redefined in PASSWORD in "script.php".

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
  • I adapted the example to focus more on the issue. I'm specifically interested in the case where variables are used and to make a file safe or include it safely. – Samuel May 25 '14 at 16:42