note 80022 deleted from function.include by danbrown

From: Date: Mon, 18 May 2009 00:57:52 +0000
Subject: note 80022 deleted from function.include by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155454@lists.php.net to get a copy of this message
Note Submitter: pepesantillan at gmail dot com ---- In response to the last post... instead of using your function to include a file, you can directly include files. But I guess my_include does something else besides including files and thats why you use it. Im learning php (just got to this part of the manual, that much of a begginer I am) but a solution I can think of (and I am posting it because your post is from about a day ago) is using an array as a parameter in the function my_include. That array would contain all your local (global) variables and would pass them to your function and made them local for that function... Heres and example (hoping its not so hard to understant) <?php //sample function, note the referencing of the array using & function my_include($file_to_include,&$my_array_of_globals) { //you can find explanation on the function extract //from the function list on this page, always check //that list, is really usefull!. Extract would go at the very //beggining of the function my_include. extract($my_array_of_globals); //now you have a kind of simulation of using global with //any single variable of your script include($file_to_include); //in this part we recreate the array containing our variables //this would go at the very end of the function $my_array_of_globals = compact(array_keys($my_array_of_globals)); } //some variables $var1 = 1; $var2 = 3; echo "First print before calling the function:<BR>\$var1 is $var1, \$var2 is $var2<BR>"; //in this part we create an array containing our variables //this is the array we will pass to the function foreach ($GLOBALS as $key => $value){ if (!is_array($value)) { //is not array? what?... yup, just to avoid post, get, cookie, etc... //and what if I have a variable that its an array? //as sad as it is for me, my method wont allow you //to get arrays back from the function $some_globals["$key"] = $value; } } //and we call our function using this array $include_string = $_SERVER["DOCUMENT_ROOT"]."/include_me.php"; //I like using include without the help of php.ini include_path my_include($include_string,$some_globals); //after that we extract our variables from $some_globals extract($some_globals); echo "Second print, after calling the function:<BR>\$var1 is $var1, \$var2 is $var2<BR>"; ?> This is include_me.php <?php // sample included file, it could try to access some variables //from the file including him and even try to change them $var1++; //from var1 = 1 to var1 = 2 $var2 *= 2; //from var2 = 3 to var2 =6 ?> and the output: First print before calling the function: $var1 is 1, $var2 is 3 Second print, after calling the function: $var1 is 2, $var2 is 6 It is not an elegant nor efficient solution, but I wanna try to help you with the little knowledge I have. Hope it is of any help! Hope someone else posts a solution! Merry Christmass to everyone

« previous php.notes (#155454) next »