Re: Extending 'include' behavior in a function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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) {
    $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!";
?>

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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux