Jump to content

Why should you not use Global $variable; ?


TeddyKiller

Recommended Posts

global $variable; in a function is very handy.

 

Although I've been told never to use them, but I'm not 100% sure why?

 

I'm sorry if I've posted a thread about this before, I just still don't understand.

 

I've searched for links and everything, but nothing that explains properly.

 

Can anyone explain? Thanks.

Link to comment
Share on other sites

because it is easy to inadvertently change this value by giving a new variable the same name of the 'global' and most likely destroying the integrity of your application there after.

 

If there are values you want to be globally available then perhaps look at using constants (define()) or creating a registry class so at the very least you can control the scope of what can amend the variable.

Link to comment
Share on other sites

Mmmm.. I understand. It's just I want to be able to go

$db->execute("query"); inside of functions.. and the only way is to pass $db as a parameter but over and over again just gets messy considering I have others too.. like $user.

That is like $user->column_name and it gets the value for that user.

 

Now using the define() method would use.. DB->execute("query") .. I think? Which I dislike the use of DB.. I always like it as $db, its just.. neater to my eye... but if it has to be used as DB instead of using 'global $db' then .. I guess I would have to do that.

 

I saw information on a registry class.. but I do not wish the use Zend_config. I would like to make my own kind of registry class... although I don't understand much about it.

Link to comment
Share on other sites

Mmmm.. I understand. It's just I want to be able to go

$db->execute("query"); inside of functions.. and the only way is to pass $db as a parameter but over and over again just gets messy considering I have others too.. like $user.

That is like $user->column_name and it gets the value for that user.

 

Like I've stated many times to others, functions/methods have argument lists for a reason.

 

ToonMariner got part of the global problem right - a global variable is a single point of failure in one's code.  If the value stored in that variable is overwritten, that change leads to a cascade of errors, many of which may be difficult to find in a complex project.

 

On the flip side of that is considering what a function/method signature does - it tells the person using it what data it needs to perform (its argument list) and what, if anything, it returns.  Globals obfuscate this communication because they're never present in the argument list.  If you're using someone else's code (like a framework), how would you know if a function/method needed a global variable to work?  You wouldn't.  So, not only is overwriting the value a possibility, never knowing that a certain value is required for a function/method to work is another.

 

Like Mchl, a singleton may be the right way to go.  A registry is essentially a specialized singleton.

 

Do yourself a favor and buy the following book: PHP Objects, Patterns, and Practice 3rd Edition.  It's essentially the tome of OOP for PHP.

Link to comment
Share on other sites

Do yourself a favor and buy the following book: PHP Objects, Patterns, and Practice 3rd Edition.  It's essentially the tome of OOP for PHP.

I just bought this book and it has a wealth of information in it. It's very good, even for a beginner in OOP (which I am).

 

Ken

Link to comment
Share on other sites

$db->execute("query"); inside of functions.. and the only way is to pass $db as a parameter but over and over again just gets messy considering I have others too.. like $user.

That is like $user->column_name and it gets the value for that user.

 

If you are using this many objects already why not switch to OOP? Like before mentioned buy PHP Objects, Patterns, and Practice. Buy Domain-Driven Design afterwards. The first book will learn you how to program OOP, the second will tell you how to do it properly.

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.