0

私はこのindex.phpファイルを持っています

# 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']);

また、loadConfiguration() は次のように設定されます。

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

database.con.php と site.con.php が config/ フォルダーにあることを確認しました。そして、私が得る唯一のエラーはNotice: Undefined variable: c in the following lineです

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

ここに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';

これが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;

私は何を間違っていますか?

4

2 に答える 2

0

d コードはrequire関数内で実行されるため$c、ローカル変数であり、グローバル スコープではアクセスできません。

派手な設計パターンを$c使用してグローバルにするか (たとえば、レジ​​ストリ (、、R::$c環境R::get('c')( $this->env->c)、...) を使用するか、構成ローダー関数を変更して、ファイルの名前とそのrequire外部を返すようにすることができます。

require_once configFile('database');

function configFile($name) {
    return path . 'config/' . $name . '.con.php';
}
于 2011-01-18T14:51:51.057 に答える
0

セキュリティ上の目的により、グローバル変数を使用しないでください。変数をレジストリ変数に設定してからアクセスできます。

于 2011-01-18T15:08:07.543 に答える