Re: [PHP] Coding Practice: Use global $var or pass in by refernce
- Original Message - From: Mark Steudel [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Friday, March 03, 2006 7:46 PM Subject: [PHP] Coding Practice: Use global $var or pass in by refernce I was wondering what the general rule on using the global driective versus passing in a variable by reference, why you should or shouldn't, etc. e.g. function () { global $db; $res = $db-query( SQL); } or function ( $db ) { $res = $db-query( SQL); } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Hi! My oponion is that is insane to use global variables. The main drawback with global variables is that is very easy to mix up variables, and keep track of what variable belongs to what. So an advice: Don't use it! /G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Coding Practice: Use global $var or pass in by refernce
Hi Gustav! Gustav Wiberg wrote: My oponion is that is insane to use global variables. The main drawback with global variables is that is very easy to mix up variables, and keep track of what variable belongs to what. So an advice: Don't use it! Ok, so what's your recommendation to solve the problem with using a DB class in many other objects/methodes? Think of a DB class: class DB {...} And a lot of classes which want to use the DB class: class Foo { function saveChangesInDb() {...} } class Bar { function saveChangesInDb() {...} } - You can use a global $db = new DB... and pass it to every class/methode, - you can make $db global in each methode, - you can create a new instance (new DB) in every methode (but you usually only want a single DB-connection per script, and where do you pass config-data to access the DB?) or - use a factory/singleton, which is not so much better than a global variable (and again, what about config-data?). So what's the way you'd recommend and why? best regards Andreas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Coding Practice: Use global $var or pass in by refernce
I would go for: - you can create a new instance (new DB) in every methode (but you usually only want a single DB-connection per script, and where do you pass config-data to access the DB?) or This way the code keeps well organized and about the use of only one connection I wouldn't worry too much since usually one thread/process is created per script called, so if this is a multiuser application you end up with several connections anyway, besides I think PHP reuses the connections when possible so the performance of your application does not seem affected at all. I use it, works perfectly with big databases and several users. Regards. Andreas Korthaus wrote: Hi Gustav! Gustav Wiberg wrote: My oponion is that is insane to use global variables. The main drawback with global variables is that is very easy to mix up variables, and keep track of what variable belongs to what. So an advice: Don't use it! Ok, so what's your recommendation to solve the problem with using a DB class in many other objects/methodes? Think of a DB class: class DB {...} And a lot of classes which want to use the DB class: class Foo { function saveChangesInDb() {...} } class Bar { function saveChangesInDb() {...} } - You can use a global $db = new DB... and pass it to every class/methode, - you can make $db global in each methode, - you can create a new instance (new DB) in every methode (but you usually only want a single DB-connection per script, and where do you pass config-data to access the DB?) or - use a factory/singleton, which is not so much better than a global variable (and again, what about config-data?). So what's the way you'd recommend and why? best regards Andreas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Coding Practice: Use global $var or pass in by refernce
- Original Message - From: Andreas Korthaus [EMAIL PROTECTED] To: php-general@lists.php.net; Gustav Wiberg [EMAIL PROTECTED] Sent: Friday, March 03, 2006 8:53 PM Subject: Re: [PHP] Coding Practice: Use global $var or pass in by refernce Hi Gustav! Gustav Wiberg wrote: My oponion is that is insane to use global variables. The main drawback with global variables is that is very easy to mix up variables, and keep track of what variable belongs to what. So an advice: Don't use it! Ok, so what's your recommendation to solve the problem with using a DB class in many other objects/methodes? Think of a DB class: class DB {...} And a lot of classes which want to use the DB class: class Foo { function saveChangesInDb() {...} } class Bar { function saveChangesInDb() {...} } - You can use a global $db = new DB... and pass it to every class/methode, - you can make $db global in each methode, - you can create a new instance (new DB) in every methode (but you usually only want a single DB-connection per script, and where do you pass config-data to access the DB?) or - use a factory/singleton, which is not so much better than a global variable (and again, what about config-data?). So what's the way you'd recommend and why? best regards Andreas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Hi Andreas! I don't have that much experience with classes, but wouldn't it work to: 1. make a connection to the db like $db = open db, and then pass around this variable? 2. Extend the DB - class, so the saveChangesInDb() - function in the Foo-class would be an extension from the DB-class ? (I think this works to extend a class in PHP right?) 3 . Use already existing classes for db...http://www.phpclasses.org/ PHP Scripts / Databases I hope this'll help! /G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Coding Practice: Use global $var or pass in by refernce
On Fri, 2006-03-03 at 14:53, Andreas Korthaus wrote: - use a factory/singleton, which is not so much better than a global variable (and again, what about config-data?). I use a global configuration to register database connection params. InterJinn uses something like the following: $GLOBALS['interJinn']['databases'] = array ( 'carnageWeb' = array ( 'type' = 'mysql', 'host' = 'wocmud.org', 'user' = 'me', 'password' = 'noyou', 'db' = 'carnage_web' ), 'carnage' = array ( 'host' = 'wocmud.org', 'user' = 'me', 'password' = 'notyouagain', 'db' = 'carnage' ), 'carnageForum' = array ( 'host' = 'wocmud.org', 'user' = 'me', 'password' = 'notyouyetagain', 'db' = 'carnage_forum' ), 'default' = 'carnageWeb', ); This is consumed by the database manager which is a singleton/factory that uses connection pooling. ?php $mDb = $this-getServiceRef( 'dbManager' ); $db = $mDb-getConnectionRef(); $db-query( 'blah blah blah' ); $db2 = $mDb-getConnectionRef(); $db2-query( 'bleh bleh bleh' ); $db3 = $mDb-getConnectionRef( 'carnageForum' ); $db3-query( 'bleh bleh bleh' ); $db-free(); $db2-free(); $db3-free(); ? In this way all database connections are named such that if something changes in the future, whether it be the name of the database, the type of database server, the port, whatever, my code shouldn't need to change -- only the configuration. Additionally since I retrieve from a pool, I only ever use as many connections as I need at a time. Be that 1 or 3 or 5. Additionally I know that when I retrieve a second database instance it's not going to clobber the query from the first request. I advocate use of the $GLOBALS array for configuration information that remains static and only when the purpose is clearly defined and well named such that you can be very sure that you won't run into naming conflicts in the near future. This is why I use an interJinn sub array within the globals to segregate InterJinn specific configuration vars. I'd advocate using constants but they don't support arrays, or at least not as far back as I maintain compatibility. Why the PHP guys didn't foresee constant array values is anyone's guess *lol*. You'll also notice I retrieve the factory/singleton by means of named service. This avoids most of that interface/inheritance bullshit that Java gets mired in (and PHP5 has embraced *hahah*). As long as the expected methods exist then any class can be dropped in as a replacement via the service registry, whether it extends, implements, burps, or farts the original class -- or not :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Coding Practice: Use global $var or pass in by refernce
So what's the way [using Globals] you'd recommend and why? best regards Andreas Andreas: I have to agree with Gustav on this -- I very seldom use Globals. I might use one for debugging, but not for finished code. Their use simply creates problems in porting code, keeping things modular, and maintenance. Perhaps I'm fortunate, but I usually find a way around using Globals. And since I've been coding in PHP, I've never been forced to use them. tedd -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Coding Practice: Use global $var or pass in by refernce
Andreas Korthaus wrote: Hi Gustav! Gustav Wiberg wrote: My oponion is that is insane to use global variables. The main drawback with global variables is that is very easy to mix up variables, and keep track of what variable belongs to what. So an advice: Don't use it! Ok, so what's your recommendation to solve the problem with using a DB class in many other objects/methodes? Think of a DB class: class DB {...} And a lot of classes which want to use the DB class: class Foo { function saveChangesInDb() {...} } class Bar { function saveChangesInDb() {...} } - You can use a global $db = new DB... and pass it to every class/methode, - you can make $db global in each methode, - you can create a new instance (new DB) in every methode (but you usually only want a single DB-connection per script, and where do you pass config-data to access the DB?) or - use a factory/singleton, which is not so much better than a global variable (and again, what about config-data?). So what's the way you'd recommend and why? IMHO, the only way a global variable would have sense is in an environment where you can be assured there's a DBClass $db instance, and that it would never ever be a $db var that's not a DBClass instance nor will $db ever be missing. So, can you guarantee this at the present and to the end of times? :) I think it would be better to create an instance of $db whatever the script you need it, and pass it to every class-constructor as a byref-parameter; i.e. $db = new DBClass(); ··· $class1 = new Class1($db); ··· $class2 = new Class2($db); Now, if you insist on using global vars, then maybe, just maybe, it would be better to let that byref-param be optional, and check in the constructor for a global DBClass instance $db if no DBClass instance was given, but then again the problem would be pretty much the same. Try not to rely on global vars, bear in mind that global vars almost always give too little info and do not reflect themselves on the function prototype --well, if you have a smart IDE/editor that understand PHPDoc comments (or something like that) and you do document their existance, it might not be that bad, but still try to avoid them. -- Atentamente, J. Rafael Salazar Magaña Innox - Innovación Inteligente Tel: +52 (33) 3615 5348 ext. 205 / 01 800 2-SOFTWARE http://www.innox.com.mx -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Coding Practice: Use global $var or pass in by refernce
On Fri, 2006-03-03 at 16:19 -0500, tedd wrote: Perhaps I'm fortunate, but I usually find a way around using Globals. And since I've been coding in PHP, I've never been forced to use them. Just my 2c... In our framework, I use globals as well, although I do tend to agree with Tedd regarding workarounds rather... :) Something like so: I create a global to hold the db object (in my case MDB2 or PEAR DB) and then use a loadclass function to get the dbconfig from another module. This creates a nice way to easily pick it up wherever its needed. I then use the factory method to instantiate the db abstraction layer and pass it, through my engine class to an object class where I further abstract the db functions through a central db class. All of the modules in the system that have database derived classes extend my dbTable class, thereby keeping everything centralised. In dbTable class, I evaluate if the db object exists, and if it *really* doesn't, I create it through the engine. This makes for lazy evaluation of the db object, so that it works kinda on demand, rather than on every request. Using this method, I can also use a global error callback method to handle _all_ database errors gracefully, using PEAR_Error. I do think that there may be a slight performance hit doing it this way, but in an app with over 180 000 lines of code (so far) I find it works just fine... --Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php