[drupal-devel] Getting rid of global variables
Hi! global variables are hard to debug and error prone. Let's make it explicit: drupal_set($module, $key, $value = NULL, $force = FALSE) { static $storage; if ($value || $force) { $storage[$module][$key] = $value; } return $storage[$module][$key]; } drupal_get($module, $key) { return drupal_set($module, $key); } $force is useful when you want to set to NULL ie. delete a value. If we can't find a revelant module for a core global value, we can use 'system'. Regards NK
I like the idea in general. Better change it to be : drupal_set($key, $value = NULL, $module='system', $force = FALSE) This way we just have key and value being mandatory. This will be useful for widespread variables like $user for example. On 9/30/05, Karoly Negyesi <karoly@negyesi.net> wrote:
Hi!
global variables are hard to debug and error prone. Let's make it explicit:
drupal_set($module, $key, $value = NULL, $force = FALSE) { static $storage; if ($value || $force) { $storage[$module][$key] = $value; } return $storage[$module][$key]; }
drupal_get($module, $key) { return drupal_set($module, $key); }
$force is useful when you want to set to NULL ie. delete a value.
If we can't find a revelant module for a core global value, we can use 'system'.
Regards
NK
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I proposed this a very long time ago ( > 2 years) Calling it context_get and context_set ... i am opposed to having using the module in the function parameters though, as it is better to be consistent with variable_get and variable_set (imo), and have coding guidelines dictate that you have to use $module_ as the prefix. On 30 Sep 2005, at 2:19 PM, Karoly Negyesi wrote:
Hi!
global variables are hard to debug and error prone. Let's make it explicit:
drupal_set($module, $key, $value = NULL, $force = FALSE) { static $storage; if ($value || $force) { $storage[$module][$key] = $value; } return $storage[$module][$key]; }
drupal_get($module, $key) { return drupal_set($module, $key); }
$force is useful when you want to set to NULL ie. delete a value.
If we can't find a revelant module for a core global value, we can use 'system'.
Regards
NK
- -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFDPT/EgegMqdGlkasRAvzDAKDg98368YzocBh8jsgSiZq8ksXuwwCgn2sZ 8WR7LctPwIYmtddN80k5MaU= =ABGA -----END PGP SIGNATURE-----
On Fri, Sep 30, 2005 at 02:19:51PM +0200, Karoly Negyesi wrote:
Hi!
global variables are hard to debug and error prone. Let's make it explicit:
drupal_set($module, $key, $value = NULL, $force = FALSE) { static $storage; if ($value || $force) { $storage[$module][$key] = $value; } return $storage[$module][$key]; }
drupal_get($module, $key) { return drupal_set($module, $key); }
How about drupal_(set|get)_global(). And drupal_(set|get)_object() can be used to get statically cached objects (nodes, terms, users, vocabularies, etc) from the db. -Neil
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 30 Sep 2005, at 7:34 PM, neil@civicspacelabs.org wrote:
How about drupal_(set|get)_global(). And drupal_(set|get)_object() can be used to get statically cached objects (nodes, terms, users, vocabularies, etc) from the db. Dude. I love that =)
but maybe call it drupal_(set|get)_static() .. pseudo code for all those functions with static's in them would be if (!($obj = drupal_get_static('obj', null)) { $obj = db_result(db_query('select blah')); drupal_set_static('obj', $obj); } - -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFDPYc+gegMqdGlkasRAl3aAJ9pWy0JD49ZSrCqAHbvo4Sw8tzYMwCgzQBC Zti81e5peWZBrJeoU0pwXdQ= =4jt9 -----END PGP SIGNATURE-----
On Fri, Sep 30, 2005 at 08:43:10PM +0200, Adrian Rossouw wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 30 Sep 2005, at 7:34 PM, neil@civicspacelabs.org wrote:
How about drupal_(set|get)_global(). And drupal_(set|get)_object() can be used to get statically cached objects (nodes, terms, users, vocabularies, etc) from the db. Dude. I love that =)
but maybe call it drupal_(set|get)_static() .. pseudo code for all those functions with static's in them would be
if (!($obj = drupal_get_static('obj', null)) { $obj = db_result(db_query('select blah'));
This will probably be some sort of callback system. $module and $type would be arguments for the generic static caching function and then call $module_get_$type().
drupal_set_static('obj', $obj); }
-Neil
participants (4)
-
Adrian Rossouw -
Karoly Negyesi -
Khalid B -
neil@civicspacelabs.org