Question

I have this index.php file

# Loading configurations.
loadConfiguration('database');
loadConfiguration('site');

# Initializing the database connection.
$db = new database($c['db']['host'], $c['db']['username'], $c['db']['password'], $c['db']['name']);
unset($c['db']['password']);

And loadConfiguration() is set as follow:

function loadConfiguration($string) { require(path.'config/'.$string.'.con.php'); }

I checked that database.con.php and site.con.php are into the config/ folder. And the only error i get is a Notice: Undefined variable: c in the following line

$db = new database($c['db']['host'], $c['db']['username'], $c['db']['password'], $c['db']['name']);

Here's the database.con.php

# Ignore.
if (!defined('APP_ON')) { die('Feel the rain...'); }

$c['db']['host'] = 'localhost';
$c['db']['username'] = 'root';
$c['db']['password'] = '';
$c['db']['name'] = 'name';

Here's the site.con.php

# Ignore.
if (!defined('APP_ON')) { die('Feel the rain...'); }

/* Site informations */
$c['site']['name'] = 'Namek';
$c['site']['mail'] = 'mail@whocares.com';
$c['site']['enable-email'] = false;
$c['site']['debug-mode'] = true;

What am i doing wrong?

Was it helpful?

Solution

The required code is executed within the function, so $c is a local variable and not accessible in global scope.

You may either use some fancy design pattern to make $c global (e.g. use a Registry (R::$c, R::get('c'), an Environment ($this->env->c), ...) or you could change your configuration loader function to return the name of the file and require outside of it:

require_once configFile('database');

function configFile($name) {
    return path . 'config/' . $name . '.con.php';
}

OTHER TIPS

Please don't use global variable according security purpose. you can set your variable in registry variable and then access it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top