note 88946 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Sat, 14 Feb 2009 17:31:05 +0000 |
| Subject: | note 88946 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150679@lists.php.net to get a copy of this message | ||
Note Submitter: m dot spada at goodbuynet dot it
----
In reply to andrew at planetubh dot com :
I used a similar function to include some php files taking their paths from a db table with a menu
tree and I got the same problem with objects: an object instance could not be recalled from insied
another Class even with global $var. In your case, instead of cycling the GLOBALS array and setting
every var as global, why not using your safeinclude function without actually doing the include but
just returning the path to the file to be included ?
You could keep every validation or check you need, but resolve the scope problem.
<?php
function safeinclude($filename)
{
//This line takes all the global variables, and sets their scope within the function:
foreach ($GLOBALS as $key => $val) { global $$key; }
/* Pre-Processing here: validate filename input, determine full path
of file, check that file exists, etc. This is obviously not
necessary, but steps I found useful. */
if ($exists==true) { return $filename); }
else {return false; }
}
$file_to_be_included = safeinclude($file_path);
include($file_to_be_included);
?>
It worked for me, now all the chain of included files have the same scope and i can recall variables
from inside any Class.