PHP Preserve scope when calling a function

Posted by Joshua on Stack Overflow See other posts from Stack Overflow or by Joshua
Published on 2008-10-03T02:33:58Z Indexed on 2010/04/17 6:53 UTC
Read the original article Hit count: 241

Filed under:

I have a function that includes a file based on the string that gets passed to it i.e. the action variable from the query string. I use this for filtering purposes etc so people can't include files they shouldn't be able to and if the file doesn't exist a default file is loaded instead. The problem is that when the function runs and includes the file scope, is lost because the include ran inside a function. This becomes a problem because I use a global configuration file, then I use specific configuration files for each module on the site. The way I'm doing it at the moment is defining the variables I want to be able to use as global and then adding them into the top of the filtering function.

Is there any easier way to do this, i.e. by preserving scope when a function call is made or is there such a thing as PHP macros?

Edit: Would it be better to use extract($_GLOBALS); inside my function call instead?

Edit 2: For anyone that cared. I realised I was over thinking the problem altogether and that instead of using a function I should just use an include, duh! That way I can keep my scope and have my cake too.

© Stack Overflow or respective owner

Related posts about php