Reputation: 4601
When comparing the following scenarios, which one would be the fastest? Sample examplary code below.
Basically, what I'm trying to find out is what's the best alternative to using global vars. The global vars that I store would be the same for all users... they are application level values which are the same for all users, application_name for example, or application_home_page_url...
I will have a ton of functions using these app level values and I do not want to pass them by argument, though I think that would be the fastest way.. My options seem to be working with either db look ups, or using session vars or globals..
Of course, you may take it to the next level by throwing the assoc. arrays into the equation to make it faster/?slower.
The other factor that we have to keep in mind is that we are talking about lots of users here.
So, what's the best way to toss around/work with these app level variables within individual functions?
//for global based approach
$my_global_var1 = "abc";
$my_global_var2 = "xyz";
//for global + array based approach
$my_globals['var1'] = "abc";
$my_globals['var2'] = "xyz";
//for session based approach
$_SESSION['my_global_var1'] = "abc"
$_SESSION['my_global_var2'] = "xyz";
//for session + array based approach
$_SESSION['my_globals']['var1'] = "abc";
$_SESSION['my_globals']['var2'] = "xyz";
//for db based approach
$varname = 'var1';
//or some other way, you may think of
//??
function func1()
{
global $my_global_var1;
global $my_global_var2;
//....
my_var1 = $my_global_var1;
}
function func2()
{
$my_var1 = $my_globals['var1'];
}
function func3()
{
$my_var1 = $_SESSION['my_global_var1'];
}
function func4()
{
$my_var1 = $_SESSION['my_globals']['var1'];
}
$my_var1 = func5($varname)
function func5($varname)
{
return lookup($varname);
}
Upvotes: 2
Views: 2422
Reputation: 2704
I would suggest using a Registry class for this.
I'm sure there is more than one way to make such a class, but i prefer using a single static class:
class Registry {
private static $values = array();
public static function set($key, $value) {
self::$values[$key] = $value;
}
public static function get($key) {
return self::$values[$key];
}
}
Then you would just include the registry at the top of your application and interface it during with Registry::set('abc', 'xyz');
and Registry::get('abc');
A few links on this technique:
http://www.devshed.com/c/a/PHP/Registry-Design-Pattern/
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html#5
Upvotes: 2
Reputation: 1401
You should pass variables to functions if possible.
Example where Global variables are used:
<?PHP
$Variable = '1234567';
function funcName()
{
global $Variable;
echo $Variable;
}
?>
or you could just do
<?PHP
$Variable = '1234567';
funcName($Variable);
function funcName($varPassed)
{
echo $varPassed;
}
?>
SESSION variables are used to store a user-specific setting or variable across multiple page loads, or pages. They are used to store session information (username,password, last page), but you have complete control of it. Also SESSION variables are stored in temp files. If the stuff your storing are for individual users, then use SESSION...MySQL will clog up if not designed right with proper indexes and such.
Upvotes: 0
Reputation: 3932
The idea behind global variables and session variables is different. Global variables are for using the same variable in a single script, regardless of scope. Session is to store variables for one user across pages. If I understand what you're trying to do correctly, it's probably better to use session variables.
Regarding the speed, try to measure your script's execution time with microtime(). You can use this (note: input not sanitized!):
<?php
$start = microtime(true);
if ($_GET['include']) { @include $_GET['include']; }
$end = microtime(true);
echo '<hr />';
echo 'The script executed in: '.(($end - $start)*1000).' milliseconds.';
?>
EDIT:
The comment to your post above me just made me understand better what you're trying to do. Indeed in case these variables are supposed to stay constant, use constants.
Upvotes: 1