TeddyKiller Posted April 27, 2010 Share Posted April 27, 2010 global $mail['footers']; This gives me.. Parse error: syntax error, unexpected '[', expecting ',' or ';' Clearly it doesn't like the ['value'] bit. So how can I prevent this, and get the same value? It's possible I could pass it as a parameter, although I'd prefer not to do that for this. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/ Share on other sites More sharing options...
roopurt18 Posted April 27, 2010 Share Posted April 27, 2010 global $mail; echo $mail['footers']; Also, you're a bad person for using globals. Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049655 Share on other sites More sharing options...
TeddyKiller Posted April 28, 2010 Author Share Posted April 28, 2010 global $mail; echo $mail['footers']; Also, you're a bad person for using globals. Ah I didn't think of that. I know globals isn't the best method, although I'm not too sure why. Can you enlighten me? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049659 Share on other sites More sharing options...
ChemicalBliss Posted April 28, 2010 Share Posted April 28, 2010 Globals tend to steer the programmer towards badly structured code. And can lead to security flaws if not properly used. Globals should be a last resort. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049665 Share on other sites More sharing options...
trq Posted April 28, 2010 Share Posted April 28, 2010 Globals should be a last resort. I would go as far as saying just avoid them all together. There really is NO NEED for them and they cause far more trouble than there worth. Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049669 Share on other sites More sharing options...
roopurt18 Posted April 28, 2010 Share Posted April 28, 2010 There's probably more reasons than I can think of off the top of my head why not to use globals, but first and foremost is they break the encapsulation of a function. Functions are intended to receive values as arguments and return values via a return statement. When you write a function without globals, you know a lot more about the variables within that function. Consider this: function foo( $a, $b, $c ) { $total = $a * $b + $c; // Let's say $total is now: 34 magical_function(); // Does $total still equal 34? Yes it does! // We know for a FACT it is 34 because it is not global and we did not assign to it. echo $total; // Still 34! } Now consider this: function foo( $a, $b, $c ) { global $total; $total = $a * $b + $c; // Let's say $total is now: 34 magical_function(); // Does $total still equal 34? We DONT KNOW! // It's global so magical_function might have altered it without our knowing. echo $total; // Might be 34! Might be something else! } Now you say, "So what?" Well when your programs are small you can easily navigate to magical_function() and see if it modifies the global $total. If your program is 10 lines or less, then maybe this is practical. As your programs increase in size, however, it becomes more and more difficult to track down the true value of the variable. Then your program will inevitably have a bug in it and because you've used globals you're going to have a difficult time tracking down the source of the problem. Not to mention you'll be very, very likely to fix A but break B. Then you'll fix B and both A and C will break. Lastly, consider that programmers are lazy and don't like typing; therefore variable names are recycled. For example I commonly use $name as a variable name. When you start introducing globals you have a hard time keeping track of the variable $name. Is this the global name? Is this a local $name? Use globals at your own peril and don't expect to be hired by any intelligent managers you submit code to with global in it. The very best reasons to not use globals are: 1) They cause problems (as I explained) 2) They're completely unnecessary. I've written some pretty complicated, functional programs that span thousands of lines of code. I've not used global once. Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049670 Share on other sites More sharing options...
TeddyKiller Posted April 28, 2010 Author Share Posted April 28, 2010 I understand. It wasn't included into a big function.. it was a simple mail function, to cut down all the headers etc. Just shove it into a function. There is a way round by using the parameter part just for the mail footer. Although I have quite a few mailers that use the function, so I stuck it as a global. I don't use globals often actually, most big functions I now turn into classes with smaller functions, there for all I'd need at the moment, is private variables. At the end of the day.. if the globals I use, cause an issue.. which may or may not happen.. then I can change it. Applications change all the time as it grows.. It's unlikely you wont run into a problem. So.. I just take it as it goes. I do understand what you're saying though. I read upon the global bit slightly last night, it basically mentioned the overwriting of variables, as $variable may be used twice without realising it. Although, there was a "tip" where you can do.. $variable = $GLOBALS['name']; Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049813 Share on other sites More sharing options...
ChemicalBliss Posted April 28, 2010 Share Posted April 28, 2010 Yes when your having to use the same function for multiple instances, you shouldnt have your function be all-knowing, it will get very messy. You should turn it into an abstract class/interface and either extend or implement variations based on what you need. Globals arent just the fact that code gets messy, as i explained earlier it can cause serious security issues. Though - Global CONSTANTS are a neat work around, they are more restrictive and safe to use. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049820 Share on other sites More sharing options...
ignace Posted April 28, 2010 Share Posted April 28, 2010 $variable = $GLOBALS['name']; is hardly a tip as it's the same as: global $name; $variable = $name; Some prefix globals with gbl (for example $gblName) as they think it's a way around the problem while it's not like Roopurt has pointed out. function foo( $a, $b, $c ) { global $total; $total = $a * $b + $c; // Let's say $total is now: 34 magical_function(); // Does $total still equal 34? We DONT KNOW! // It's global so magical_function might have altered it without our knowing. echo $total; // Might be 34! Might be something else! } Globals are bad no matter how small the application. Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049823 Share on other sites More sharing options...
KevinM1 Posted April 28, 2010 Share Posted April 28, 2010 Functions contain argument lists for a reason. There is never a good reason to use global. Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1049851 Share on other sites More sharing options...
TeddyKiller Posted April 29, 2010 Author Share Posted April 29, 2010 Functions contain argument lists for a reason. There is never a good reason to use global. May I ask.. if globals are so bad, why hasn't PHP deprecated them? Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1050441 Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2010 Share Posted April 29, 2010 Php has been slow to mitigate the problems and remove any of the early unfortunate choices that were put into the language. And in fact, they don't consider many of the things that have been shown to actually hinder and waste time when writing real life actual applications to be a problem. The following is a quote taken from a section of the php.net documentation concerning one of the unfortunate choices that is finally being removed - However, they did exist and did help a few beginners blissfully and unknowingly write better (more secure) code. (strike-though added by me) There is a big difference between writing code that just barely produces the result you want for an assignment and efficiently writing real life actual applications that work well under all conditions. Programming languages should be, well, programming languages. They should not automatically manipulate data, especially when you don't want them to manipulate the data. They should not pollute variable scopes (which is where this thread falls under) and overwrite variables, especially when you don't want them to overwrite variables. They should not hide poor syntax in code. They should not have more than one tag that defines what code is and make one tag optional so that code is no longer seen as code on some servers. Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1050452 Share on other sites More sharing options...
roopurt18 Posted April 29, 2010 Share Posted April 29, 2010 May I ask.. if globals are so bad, why hasn't PHP deprecated them? Probably due to historical and backwards-compatibility reasons. Almost every C-based programming language has globals and many other languages as well. I've had many programming courses taught by people with years of programming experience and I've read many programming books. When it comes to globals both resources pretty much say, "This is a global. Now it can be used anywhere. That said, don't use globals." And they're right. Quote Link to comment https://forums.phpfreaks.com/topic/199988-function-globals/#findComment-1050531 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.