RE: [PHP] include/require inside of function
-Original Message- From: Ow Mun Heng [mailto:[EMAIL PROTECTED] Sent: 09 July 2003 03:44 I finally got it. Thanks. All I needed to do was just define global $page_title inside the function to denote that I wanted to use that variable. One other quick question, in my original code (per below) the function create_global() was used to create_globals on demand. But this involved a lot of jumping, from one function to another just to create the globals on demand. Is this efficient (more?? less??) compared to just stating global $page_title inside the function? I don't think create_global() is of any use to you in this situation -- you're just wasting the time taken to do the function call and return. Just do either: function org_display_title_code() { global $page_title; echo 'This is title1 - ' . $page_title; } Or: function org_display_title_code() { echo 'This is title1 - ' . $GLOBALS['page_title']; } Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] include/require inside of function
Hi Mike, I realise that last night. I'm now using $GLOBALS['page_title'] In what situation would create_global() be useful then? Thanks. Cheers, Mun Heng, Ow H/M Engineering Western Digital M'sia DID : 03-7870 5168 -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 09, 2003 6:20 PM To: Ow Mun Heng; Ford, Mike [LSS] Cc: [EMAIL PROTECTED] Subject: RE: [PHP] include/require inside of function -Original Message- From: Ow Mun Heng [mailto:[EMAIL PROTECTED] Sent: 09 July 2003 03:44 I finally got it. Thanks. All I needed to do was just define global $page_title inside the function to denote that I wanted to use that variable. One other quick question, in my original code (per below) the function create_global() was used to create_globals on demand. But this involved a lot of jumping, from one function to another just to create the globals on demand. Is this efficient (more?? less??) compared to just stating global $page_title inside the function? I don't think create_global() is of any use to you in this situation -- you're just wasting the time taken to do the function call and return. Just do either: function org_display_title_code() { global $page_title; echo 'This is title1 - ' . $page_title; } Or: function org_display_title_code() { echo 'This is title1 - ' . $GLOBALS['page_title']; } Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] include/require inside of function
-Original Message- From: Ow Mun Heng [mailto:[EMAIL PROTECTED] Sent: 07 July 2003 04:34 Here's My question, a variable is not actually global is not actually global until I make it global through global $make_this_global and then I can assess it using $GLOBAL[$make_this_global]. Not correct. Variables used in the global scope are global, and appear in the $GLOBALS array as soon as they come into existence. A global statement is used within a function merely to declare that you wish to use one of those variables in that function, where it would otherwise not be available. It's sort of equivalent to saying when I use variable $x in this function, I actually want it to be $GLOBALS['x'] -- or, in PHP terms, $x = $GLOBALS['x']. Another method would be to globalise it on demand by writing a little function. (like Rasmus) I did it like this -- ---create_globals.php function create_global($passed_variable) if (isset ($GLOBALS[$passed_variable])) { return $GLOBALS[$passed_variable]; } ---end- config.php-- $page_title = Page Title of Web Page -end --index.html require_once ('create_globals.php'); $local_variable = create_global( 'main_title') echo My Page Title is.$local_variable; --end- Hence This way I can make it global-on-demand. Maybe a better way to do it would be to ---config.php--- global $page_title = Alternative Way end- I'm really not sure what you're getting at here, but I think it may be based on a misunderstanding. If you could explain exactly what it is you're trying to do, someone might be able to offer some better help. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] include/require inside of function
Hi Mike, Here's the thing.. I declared $page_title = My Page Title in a file called config.php which is included in the index.php page. when I tried to - echo $page_title - Nothing comes out. If I declared it as global $page_title , then My Page Title would be echoed out. That I think is the simplest way to explain it. Cheers, Mun Heng, Ow H/M Engineering Western Digital M'sia DID : 03-7870 5168 -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 08, 2003 7:11 PM To: Ow Mun Heng Cc: [EMAIL PROTECTED] Subject: RE: [PHP] include/require inside of function -Original Message- From: Ow Mun Heng [mailto:[EMAIL PROTECTED] Sent: 07 July 2003 04:34 Here's My question, a variable is not actually global is not actually global until I make it global through global $make_this_global and then I can assess it using $GLOBAL[$make_this_global]. Not correct. Variables used in the global scope are global, and appear in the $GLOBALS array as soon as they come into existence. A global statement is used within a function merely to declare that you wish to use one of those variables in that function, where it would otherwise not be available. It's sort of equivalent to saying when I use variable $x in this function, I actually want it to be $GLOBALS['x'] -- or, in PHP terms, $x = $GLOBALS['x']. Another method would be to globalise it on demand by writing a little function. (like Rasmus) I did it like this -- ---create_globals.php function create_global($passed_variable) if (isset ($GLOBALS[$passed_variable])) { return $GLOBALS[$passed_variable]; } ---end- config.php-- $page_title = Page Title of Web Page -end --index.html require_once ('create_globals.php'); $local_variable = create_global( 'main_title') echo My Page Title is.$local_variable; --end- Hence This way I can make it global-on-demand. Maybe a better way to do it would be to ---config.php--- global $page_title = Alternative Way end- I'm really not sure what you're getting at here, but I think it may be based on a misunderstanding. If you could explain exactly what it is you're trying to do, someone might be able to offer some better help. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] include/require inside of function
-Original Message- From: Ow Mun Heng [mailto:[EMAIL PROTECTED] Sent: 08 July 2003 15:53 Here's the thing.. I declared $page_title = My Page Title in a file called config.php which is included in the index.php page. when I tried to - echo $page_title - Nothing comes out. If I declared it as global $page_title , then My Page Title would be echoed out. And is the assignment in the included file inside a function? If so, the usual rules about variables within functions apply. If not, please post the relevant code from both files so we can try to figure out your problem. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] include/require inside of function
Hi Mike, I finally got it. Thanks. All I needed to do was just define global $page_title inside the function to denote that I wanted to use that variable. One other quick question, in my original code (per below) the function create_global() was used to create_globals on demand. But this involved a lot of jumping, from one function to another just to create the globals on demand. Is this efficient (more?? less??) compared to just stating global $page_title inside the function? Please enlighten me. ---config--- $page_title = Main Page Title; -- ---functions- function display_title() { global $page_title; This was what I missed echo $page_title; } function org_display_title_code() --- That was what I used originally { $l_main_title = create_globals( 'page_title' ); echo 'This is (create global) title1 - ' . $l_main_title ; } function create_globals( $passed_option ) { if ( isset( $GLOBALS[$passed_option] ) ) { return $GLOBALS[$passed_option]; } } --- --index.php-- ?php display_title(); ? Cheers, Mun Heng, Ow H/M Engineering Western Digital M'sia DID : 03-7870 5168 -Original Message- From: Ford, Mike [LSS] [mailto:[EMAIL PROTECTED] Sent: Tuesday, July 08, 2003 11:34 PM To: Ow Mun Heng Cc: [EMAIL PROTECTED] Subject: RE: [PHP] include/require inside of function -Original Message- From: Ow Mun Heng [mailto:[EMAIL PROTECTED] Sent: 08 July 2003 15:53 Here's the thing.. I declared $page_title = My Page Title in a file called config.php which is included in the index.php page. when I tried to - echo $page_title - Nothing comes out. If I declared it as global $page_title , then My Page Title would be echoed out. And is the assignment in the included file inside a function? If so, the usual rules about variables within functions apply. If not, please post the relevant code from both files so we can try to figure out your problem. Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] include/require inside of function
Hmm.. Funny I was having this same problem yesterday and wanted to post. Here's My question, a variable is not actually global is not actually global until I make it global through global $make_this_global and then I can assess it using $GLOBAL[$make_this_global]. Another method would be to globalise it on demand by writing a little function. (like Rasmus) I did it like this -- ---create_globals.php function create_global($passed_variable) if (isset ($GLOBALS[$passed_variable])) { return $GLOBALS[$passed_variable]; } ---end- config.php-- $page_title = Page Title of Web Page -end --index.html require_once ('create_globals.php'); $local_variable = create_global( 'main_title') echo My Page Title is.$local_variable; --end- Hence This way I can make it global-on-demand. Maybe a better way to do it would be to ---config.php--- global $page_title = Alternative Way end- then I can -echo This is an .$page_title; and it'll come out as - This is an alternative way The only problem here would be the EXTRA typing involved. The other problem/question is, will there be a speed tradeoff? If I were to global'ise everything at the 1st place, would it be faster than jumping in and out of create_global() and going through $page_title whatever other variables? (I want to make all global configurations in this file so it'll be easy to change) Any help is appreciated. Cheers, Mun Heng, Ow H/M Engineering Western Digital M'sia DID : 03-7870 5168 -Original Message- From: Greg Beaver [mailto:[EMAIL PROTECTED] Sent: Saturday, July 05, 2003 12:39 AM To: Rasmus Lerdorf Cc: Aric Caley; [EMAIL PROTECTED] Subject: Re: [PHP] include/require inside of function Hi, If the file you are including is of your own authorage (I know that isn't a word, but whatever :), just refer to global variables always as an index of the $GLOBALS array. This is good practice anyways for any file that might be included by another user, for exactly this issue. I have a file that could be a global include or not in my project, and making sure every global variable is referenced as $GLOBALS['varname'] fixed it for me without any fancy code (although Rasmus's code is very nice) Regards, Greg -- phpDocumentor http://www.phpdoc.org Rasmus Lerdorf wrote: On Fri, 4 Jul 2003, Aric Caley wrote: Is there anyway to include a file inside of a function and have the included stuff be global? For instance if I defined a class or a function in that include file, I want to be able to use that class outside of the function. On the documentation for include() a poster commented that it did indeed work like this, but my testing indicates it does not. Everything stays local to the function and goes away when the function ends. Is there a way? Functions defined in included files are always global. So I guess it is just the variable you want to put out into the global symbol table. It's a little bit tricky, but you can do it like this: function foo($filename) { extract($GLOBALS, EXTR_REFS); include $filename; $arr = array_diff(get_defined_vars(),$GLOBALS); foreach($arr as $var=$val) $GLOBALS[$var] = $val; } -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] include/require inside of function
Is there anyway to include a file inside of a function and have the included stuff be global? For instance if I defined a class or a function in that include file, I want to be able to use that class outside of the function. On the documentation for include() a poster commented that it did indeed work like this, but my testing indicates it does not. Everything stays local to the function and goes away when the function ends. Is there a way? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] include/require inside of function
On Fri, 4 Jul 2003, Aric Caley wrote: Is there anyway to include a file inside of a function and have the included stuff be global? For instance if I defined a class or a function in that include file, I want to be able to use that class outside of the function. On the documentation for include() a poster commented that it did indeed work like this, but my testing indicates it does not. Everything stays local to the function and goes away when the function ends. Is there a way? Functions defined in included files are always global. So I guess it is just the variable you want to put out into the global symbol table. It's a little bit tricky, but you can do it like this: function foo($filename) { extract($GLOBALS, EXTR_REFS); include $filename; $arr = array_diff(get_defined_vars(),$GLOBALS); foreach($arr as $var=$val) $GLOBALS[$var] = $val; } -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] include/require inside of function
Hi, Friday, July 4, 2003, 5:11:18 PM, you wrote: AC Is there anyway to include a file inside of a function and have the included AC stuff be global? For instance if I defined a class or a function in that AC include file, I want to be able to use that class outside of the function. AC On the documentation for include() a poster commented that it did indeed AC work like this, but my testing indicates it does not. Everything stays AC local to the function and goes away when the function ends. AC Is there a way? One trick that works in php-4.X.X is to put your function inside a class, as any sub functions become global (invisible to the rest of the class though :) but that should not matter). Not sure whether it will be the same in php-5. a quick example (ignore my filenames it was just to prove it works) ?php class loadClass { function loadClass(){ //nothing to do yet; } function load($inc){ include($inc); } } $l = new loadClass(); $l-load('templateClass.inc'); $t = new kwikTemplateClass('./'); $variables = array(); echo $t-processFile('layout.htm',$variables); ? This loads the class I need and it is automatically global.. -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] include/require inside of function
Hi, If the file you are including is of your own authorage (I know that isn't a word, but whatever :), just refer to global variables always as an index of the $GLOBALS array. This is good practice anyways for any file that might be included by another user, for exactly this issue. I have a file that could be a global include or not in my project, and making sure every global variable is referenced as $GLOBALS['varname'] fixed it for me without any fancy code (although Rasmus's code is very nice) Regards, Greg -- phpDocumentor http://www.phpdoc.org Rasmus Lerdorf wrote: On Fri, 4 Jul 2003, Aric Caley wrote: Is there anyway to include a file inside of a function and have the included stuff be global? For instance if I defined a class or a function in that include file, I want to be able to use that class outside of the function. On the documentation for include() a poster commented that it did indeed work like this, but my testing indicates it does not. Everything stays local to the function and goes away when the function ends. Is there a way? Functions defined in included files are always global. So I guess it is just the variable you want to put out into the global symbol table. It's a little bit tricky, but you can do it like this: function foo($filename) { extract($GLOBALS, EXTR_REFS); include $filename; $arr = array_diff(get_defined_vars(),$GLOBALS); foreach($arr as $var=$val) $GLOBALS[$var] = $val; } -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php