Re: Extending 'include' behavior in a function
| From: | Chris | Date: | Thu, 11 Jan 2007 01:05:03 +0000 |
| Subject: | Re: Extending 'include' behavior in a function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-246881@lists.php.net to get a copy of this message | ||
Eli wrote:
Hi, I want to create a function that does some stuff, and then includes another file in the place from where it was called. The idea is to run the included file not in the function, but in the place of the caller in the caller's environment (not in the function environment). For example: <?php function includeIt($file) {Try $name = "Deb"; .. function includeIt($file) { global $name; include($file); } ... does that work? The problem is to do with variable scope (see http://www.php.net/manual/en/language.variables.scope.php). -- Postgresql & php tutorials http://www.designmagick.com/$name = "Debora"; trigger_error("Include START: '$file'"); include($file); // This include should run in the environment // of the caller, from the place it was called. trigger_error("Include END: '$file'");} $name = "Joe"; echo "Hello!";includeIt("msg.inc"); // The file should be included here, not in the // 'includeIt' function. // Say the file 'msg.inc' uses the $name // variable from the caller's environment. // In this case, I want it to say hello to Joe, // and not to Debora.echo "Bye!"; ?>