I'm not so advanced with PHP and am looking for the best solution to the following:
I need to RE-USE this frequently in my script.
$my_settings = array('width'=>'300', 'height'=>'200', ...);
I store that in a MySQL db.
The PHP script needs to get that array from MySQL once each time it runs in order to know how to compose the view.
(The array will eventually become quite large... Maybe few hundred rows...)
What is the best practice to get and define this array only ONCE in the beginning of my script, so I can re-use it all the time by other functions etc...?
Is it by defining it globally?
global $my_settings;
$my_settings = get_my_settings_from_db();
function returnSetting($key='') {
global $my_settings;
return $my_settings[$key];
}
Or is this bad idea?
What is the most efficient way to do it?
Thanks!
Instead of using global
(which is a bad thing to do) you should inject the variable (or even better the part you need into the function that needs it):
$my_settings = get_my_settings_from_db();
function returnSetting($settings, $key='') {
return $settings[$key];
}
returnSetting($my_settings, $key);
Or perhaps better create a settings class:
class Settings
{
protected $settings;
public function __construct($settings)
{
$this->settings = $settings;
}
public function getSetting($key)
{
return $this->settings[$key];
}
}
// you only create one instance per request, so you would do this in for example your bootstrap file
$settings = new Settings($my_settings);
Let's say you have a class which needs some settings you could do the following:
class Foo
{
protected $settings;
public function __construct($settings)
{
$this->settings = $settings;
{
public function bar()
{
// do something which requires a setting
echo $this->settings->getSetting('dimensions');
}
}
$settings = new Settings($my_settings);
$foo = new Foo($settings);
$foo->bar();
When doing it this you will make you code better testable and maintainable.
Note this is just something I written with a beer in one hand and it is untested, but you should get the idea. :-)
You don't need the first global like this:
$my_settings = get_my_settings_from_db();
function returnSetting($key='') {
global $my_settings;
return $my_settings[$key];
}
Global is nice! <- it is what I will say if Today was July, 2002
But in the 2012 global must be killed by all of the Ninja devs.
Try to use a static var for this:
class DataCollector{
static $my_settings = array('width'=>'300', 'height'=>'200');
}
//You could use the values at ANY place or function
echo DataCollector::$my_settings['width'];
Also read about OOP basics.
function getSettings($id, $type) {
if(!$id == 0) {
$sql = "SELECT * FROM 'database' WHERE 'id' = '".$id."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0) {
while($row = mysql_fetch_assoc($result)) {
if($type == "setting1") {
return $row['setting1'];
} elseif($type == "setting2") {
return $row['setting2'];
} elseif($type == "setting3") {
return $row['setting3'];
} elseif($type == "setting4") {
return $row['setting4'];
} else {
return "Wrong Input";
}
} } else { return "Not Initialized";}
else { return "No $id input"; }}}
Store login in $_SESSION
and call with getSetting($_SESSION['id'], "setting1")
$GLOBAL is a php super global variable which can be used instead of 'global' keyword to access variables from global scope, i.e. the variables which can be accessed from anywhere in a php script even within functions or methods.
$my_settings = array('width'=>'300', 'height'=>'200', ...);
function blablabla() {
$b = $GLOBALS['my_settings'] ;
echo $b ;
}