Is it possible to make all function's vars global without typing all of them like global $a, $b, $c...
?
views:
113answers:
4Why awful? Can you prove it? I need this for my one function.
hey
2010-05-30 21:06:17
You only need it because the rest of your design sucks. How about encapsulating all the data you need in an object, instead of polluting the global variable space with them?
Matti Virkkunen
2010-05-30 21:11:11
In regards to Matti, Are you referring to a method such as my post above!
RobertPitt
2010-05-31 16:06:51
+2
A:
You can pass them as arguments and then you won't need the global
keyword:
function(&$a, &$b, &$c)
{
// your code........
}
Sarfraz
2010-05-30 21:04:29
@Artefacto. Good call. But IMO turning args into "out parameters" is no better than using globals in the first place. And perhaps it's worse in that if you read the line that calls the function you have no hint that the parameter can be modified by the function.
Juan Pablo Califano
2010-05-30 22:02:57
@Juan Pablo Califano: Agreed on that, no hard and fast need for this, using global keyword should be the way to go.
Sarfraz
2010-05-30 22:14:06
+4
A:
Try creating a Static object within your application and assigning variables to that scope, Like so!
<?php
/*
* Core class thats used to store objects of ease of access within difficult scopes
*/
class Registry
{
/*
* @var array Holds all the main objects in an array a greater scope access
* @access private
*/
private static $objects = array();
/**
* Add's an object into the the global
* @param string $name
* @param string $object
* @return bool
*/
public static function add($name,$object)
{
self::$objects[$name] = $object;
return true;
}
/*
* Get's an object out of the registry
* @param string $name
* @return object on success, false on failure
*/
public static function get($name)
{ if(isset(self::$objects[$name]))
{
return self::$objects[$name];
}
return false;
}
/**
* Removes an object out of Registry (Hardly used)
* @param string $name
* @return bool
*/
static function remove($name)
{
unset(self::$objects[$name]);
return true;
}
/**
* Checks if an object is stored within the registry
* @param string $name
* @return bool
*/
static function is_set($name)
{
return isset(self::$objects[$name]);
}
}
?>
Considering this file is one of the first files included you can set any object/array/variable/resource/ etc into this scope.
So lets say you have just made a DB Connection, this is hwo you use it
...
$database = new PDO($dns);
Registry::add('Database',$database);
$DBConfig = Registry::get('Database')->query('SELECT * FROM config_table')->fetchAll(PDO::FETCH_CLASS);
Registry::add('Config',$DBConfig);
No anywhere else within your script you can add or retrieve items.
with Registry::get('ITEM_NEEDED');
This will work in methods functions etc.
Perfect example
function insertItem($keys,$values)
{
Registry::get('Database')->query('INSERT INTO items ('.implode(',',$keys).') VALUES ('.implode(',',$values).')');
}
Hope it helps
RobertPitt
2010-05-30 21:21:47
I don't mean to be rude, but this has almost all of the problems of using globals, except it takes more typing (which seems to be the main problem for the OP).
Juan Pablo Califano
2010-05-30 21:58:07
This is the way i prefer to do things, If you have a better solution then please feel free to post it.
RobertPitt
2010-05-30 22:00:51
@RobertPitt. That's fine and your answer is perfectly valid. I just meant that relying on global state to pass data around can become problematic quite easily. I'd rather use parameters and return values.
Juan Pablo Califano
2010-05-30 22:11:52
I Know and I understand, But if you done `$DB = new DB;` its the same as Registry::add('DB',new DB); because all the registry is doing is storing items in a variable within a broader scope, infact using my method takes up less variable storage because all objects are stored in 1 varaible, and if your using the system like Registry::get('DB')->query() your not even assigning the object to a new var so its more streamline and less hassle IMO, you could expand the code so you can set objects into sections like NS such as Registry::add('/System/Abstraction/MySql',new Abstraction('mysql'));
RobertPitt
2010-05-30 22:19:23
You're probably right about this approach saving a few bytes here and there if you want to make some data accessible to every piece of code in you app. But what I wanted to stress (as you understand) is that giving read/write access (so to speak) to some piece of data to everyone can become hard to manage easily.
Juan Pablo Califano
2010-05-30 22:42:39
+1
A:
You can always use $GLOBALS["var"]
instead of $var
. Of course, if you need this, you're most likely doing it wrong.
Artefacto
2010-05-30 21:26:45