becoming insane: $GLOBALS not working in an included file

From: Date: Fri, 08 Sep 2000 13:57:22 +0000
Subject: becoming insane: $GLOBALS not working in an included file
Groups: php.general 
Request: Send a blank email to php-general+get-15877@lists.php.net to get a copy of this message
I have spent most of the day trying to make this work. I really cannot understand why this does not work... I'm getting a bit lost in scope problems at the moment, especially what happens to scope when yu call an include I'm using the new php-4.0.2 with phplib and am trying to keep my layout separate from my "real" code. So i have routines which call "template files" - the template files themselves are mostly html with a few bits of <?php print $somevariable ?> in them. So I have a class page, it has attributes such as "menu" and "content" which are paths to content files it has a method generate() which mostly includes a template, which builds the page and includes the right menu and content page (as defined in variables). My problem: I cannot access any of the global variables, or the GLOBALS array from the content pages. It's like they completely vanish from scope. What happens in my program is: main program -calls- class function -includes- template file -incudes- content file. Now I just assumed the code in include files just is in the same scope as the lines that is doing the include. But obviously not, as I can do an 'echo $avariable;' in the template file fine, but find no way to do the same from the content file which is called inside the template file. Neither $GLOBALS not global $avariable will do it... it-s like those variables vanish... same thing for the phplib session $sess etc. help? Joelle Nebbe bits of code to maybe help see why it's so odd... --- main program --- // opens a phplib session -> global variables containing the stuff i want // receives a ton of variables from a form // does a few other things among which define $avariable and register it $mypage = new page; $mypage->setcontent(somefilename); $mypage->setmenu(somefilename); $mypage->generate(); --- page class --- var $fields = array(); var $imagepath = somepath; var $content; generate() { include (TEMPLATE_FILE_PATH); // defined in a config file } ------------------------- --- this is pagetemplate.phtml --- // HTML, mostly // ALL THE VARIABLES DEFINED IN MAIN ARE ACCESSIBLE EITHER DIRECTLY // OR THROUGH global ... or $GLOBALS // build table in html // then: <?php print $avariable // is happy include ("$this->content"); ?> ---------------------------- --- this is the content file called through $this->content --- // again, mostly HTML // NOTHING WORKS TO GET SOME OF THE VARIABLES DEFINED IN THE MAIN // PROGRAM <?php print $avariable // is unhappy - doing global $avariable does not work either print $GLOBALS["avariable"] // is unhappy global $sess // does not work etc.etc.etc. ?> ---------------------------- Joelle Nebbe Senior Software Architect - Regional Interactive Media, Leeds, UK E-mail: joelle.nebbe@rim.co.uk _______________________________________________________________________ Any opinions expressed in the email are those of the individual and not necessarily the company. This email and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivering to the intended recipient, be advised that you have received this email in error and that any use is strictly prohibited. If you have received this email in error please notify the IT manager by telephone on 0113 243 2701 _______________________________________________________________________

« previous php.general (#15877) next »