stijn0713 Posted March 29, 2012 Share Posted March 29, 2012 I have some questions about the scope of navigation : if i define a constant like define('constant'), php manuel says it's global, does this mean that i can use of different files also? like, i defined in define.php and used in anotherfile.php, or shoud the latter file be included then? what about file a.php includes file b.php which in turn includes file c.php. Can functions of a use variables of file c? ultimately: what about variables that are declared global inside a function but this variables is not initialized in that script, like in wordpress: function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { global $wp_locale; // more code } $wp_locale can be initialized in another script file? thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/259949-visibilityscope-of-variables-and-functions/ Share on other sites More sharing options...
Muddy_Funster Posted March 29, 2012 Share Posted March 29, 2012 On the back of another thread with similar questions - ignore the word global, don't even think about coding with it. Right, on to your other questions, first some sample code for ilustration: <?php $name = getName('my Name'); include 'page2.php'; //page2.php has the function getName($name) in it $anotherName = getName('my Name'); ?> $name will fail, $anotherName will work. The code is efectivly parsed in a top down manner, so it will process the $name line then process every line in the page2.php then process the $anotherName line. This means that PHP has no idea about the getName() function untill after it has passed the $name line. However, PHP can "scan" the current page for the function definition and run it from anywhere on the same page as it is currently processing. This is an over simplification, but it should give you the idea. If the file is not included, and there is no other process used to access the code contained within that file then the current code file has no knowledge of it. Except when dealing with class libraries, these are preloaded at runtime and are accessable on any page. it is in these libraries that all of php's in built functions reside, hence why you don't have to include or refference any of these files directly. You can autoload your own libraries as well, but that's another topic. Quote Link to comment https://forums.phpfreaks.com/topic/259949-visibilityscope-of-variables-and-functions/#findComment-1332363 Share on other sites More sharing options...
stijn0713 Posted March 29, 2012 Author Share Posted March 29, 2012 Ok, that example is quite straightforward. then i suppose wordpress works with these preloaded libraries because i constantly see functions and variables in scripts that are not initialized there and nor is there an include or require to files that contain these variables. ill be digging in those libraries then thanks Quote Link to comment https://forums.phpfreaks.com/topic/259949-visibilityscope-of-variables-and-functions/#findComment-1332376 Share on other sites More sharing options...
Psycho Posted March 29, 2012 Share Posted March 29, 2012 On the back of another thread with similar questions - ignore the word global, don't even think about coding with it. I would agree that - in most situations - you should not use the global keyword to make a variable global. But, defining constants - which will be global - is not a bad practice. And, many variables are global by default. So, I would not say to ignore the 'word' global. if i define a constant like define('constant'), php manuel says it's global, does this mean that i can use of different files also? like, i defined in define.php and used in anotherfile.php, or shoud the latter file be included then? When a user makes a request for a 'page' there may be any number of scripts that are executed to generate the page. Once a constant is defined any additional code that is run can reference that constant. It does not mean that you can define a constant on one page load and then have that constant available in subsequent page loads. It would need to be defined on any page request that you need it - and it does not retain it's value from previous page loads. what about file a.php includes file b.php which in turn includes file c.php. Can functions of a use variables of file c? The easiest way to understand how includes work is this. Imagine that wherever there is an include (or require) that the system is copying the code from the include file and pasting it into the file that included it. Anything that occured in earlier files will affect any code in included files just as if they were in the same script. what about variables that are declared global inside a function but this variables is not initialized in that script, like in wordpress: function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { global $wp_locale; // more code } $wp_locale can be initialized in another script file? Depends on what you mean. If that script/file is included() during the same execution that the function was run then, yes, it would be available. If it is a different page load, then no. But, as muddy_funster stated, this is considered poor practice and I agree. You should read through this page in the manual: http://us3.php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/259949-visibilityscope-of-variables-and-functions/#findComment-1332379 Share on other sites More sharing options...
stijn0713 Posted April 4, 2012 Author Share Posted April 4, 2012 ok thanks for the asnwers, about the keyword global: i've seen more people saying it's considered a bad practice. Why exactly? I see it alot of times being used in functions in wordpress, are you guys suggesting that's bad software then? (neutrally said ) Quote Link to comment https://forums.phpfreaks.com/topic/259949-visibilityscope-of-variables-and-functions/#findComment-1334372 Share on other sites More sharing options...
AyKay47 Posted April 4, 2012 Share Posted April 4, 2012 about the keyword global: i've seen more people saying it's considered a bad practice. Why exactly? It completely breaks the encapsulation of a function etc by introducing variables from the global scope.. Wordpress is poorly coded.. Quote Link to comment https://forums.phpfreaks.com/topic/259949-visibilityscope-of-variables-and-functions/#findComment-1334396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.