note 88946 added to language.variables.scope

From: Date: Sat, 14 Feb 2009 17:28:26 +0000
Subject: note 88946 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-150678@lists.php.net to get a copy of this message
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. ---- Server IP: 80.241.173.254 Probable Submitter: 213.156.60.26 ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- https://master.php.net/note/edit/88946 Del: integrated -- https://master.php.net/note/delete/88946/integrated Del: useless -- https://master.php.net/note/delete/88946/useless Del: bad code -- https://master.php.net/note/delete/88946/bad+code Del: spam -- https://master.php.net/note/delete/88946/spam Del: non-english -- https://master.php.net/note/delete/88946/non-english Del: in docs -- https://master.php.net/note/delete/88946/in+docs Del: other reasons-- https://master.php.net/note/delete/88946 Reject -- https://master.php.net/note/reject/88946 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150678) next »