Re: Extending 'include' behavior in a function

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

 



short of playing around with the global keyword
(which won't work when you call includeIt() from
another function)) your pretty much stuck.

basically imho you need to rethink what it is your trying to do.

function have their own scope for a reason; maybe consider using
an array argument like so:

function incIt($file, $args = array())
{
	extract((array)$args);
	include $file; // don't forget some error checking
}

function includeIt($file, $args = array())
{
	// bla
	incIt($file, $args);
	// more bla
}

I broke it down into 2 functions to avoid local variables
being overridden in includeIt() [by the call to extract].

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!";
> ?>
> 
> -thanks, Eli.
> 

-- 
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