View Full Version : help with a config updater function

01-31-2007, 06:21 AM
ok, basically what i want is this. i want a global config file, with a $config[] array, and a function to update that array. such as.


$config['name'] = "Justin";

function changeSetting($setting, $value) {

$config[$setting] = $value;



when i include that into my page, and run the following, it stays as 'Justin'

echo $config['name'];
changeSetting("name", "Bob");
echo $config['name'];

and i doing this right, or do i need to make this into a class...

coming from asp.net, im willing to learn oop in php.

Thanks, Justin

01-31-2007, 06:57 AM
Why not just change the value directly without the use of a function. The function in itself without being inside of a class is not really oop anyway. if you really want oop try this (based on php4):

class configKey {
function configKey($key,$value) {

function setKey($key) {
$this->key = $key;

function setValue($value) {
$this->value = $value;

You can then use it like so

$config['name'] = 'Justin';
$cfgObj = new configKey('name','Bob');
$config[$cfgObj->key] = $cfgObj->value;

*I'm fairly new with PHP as well. I've been programming less than 1 year in any given language but have picked up php on my own. I'm getting a better grasp on OOP though I know there is still much more that I need to learn. I welcome better suggestions so that I may learn as well from more seasoned programmers.

01-31-2007, 09:25 AM
the reason your function doesn't work is that $config you're setting inside the function isn't the same $config that's in the rest of your page.

What you've started to do makes sense, in that setting values directly to the array is a little 'brittle', but the post above is hinting towards a better way, but is (in my opinion) a little off...
I would use something like:

class Config {
private $data = array();
public function __construct() {


public function set($key,$val) {

public function get($key) {
if(isset($this->data[$key])) {
return $this->data[$key];
return false;

and then you would use

$config = new Config();
echo $config->get('name');

I would probably make this class into a singleton (http://www.developertutorials.com/tutorials/php/php-singleton-design-pattern-050729/page1.html) but it's a good start

01-31-2007, 12:07 PM

Thanks for the reply, i think i will be using this, i am really trying to get my head into oop with php.

Thanks alot, Justin