Jump to content

Function Globals


TeddyKiller

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.