TeddyKiller Posted June 26, 2010 Share Posted June 26, 2010 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 26, 2010 Share Posted June 26, 2010 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. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted June 26, 2010 Author Share Posted June 26, 2010 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted June 26, 2010 Share Posted June 26, 2010 For storing database connection a singleton pattern might be something you could use. Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 26, 2010 Share Posted June 26, 2010 And as much as possible, avoid using "variable anywhere". If you have values that you want to be readily available and is quite usable in the whole series of lines of codes of files, use constants instead. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted June 26, 2010 Share Posted June 26, 2010 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 26, 2010 Share Posted June 26, 2010 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 Quote Link to comment Share on other sites More sharing options...
ignace Posted June 26, 2010 Share Posted June 26, 2010 $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. Quote Link to comment 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.