2011-09-30 126 views
0

我遇到這個問題,通過名稱獲取變量。我正在做的是包括一個連接路徑,我希望連接以某種方式命名。我想遍歷這些條件並將它們添加到隊列中,但問題是,我無法使用'。'。連接一個變量。PHP通過名稱和連接獲取變量

代碼看起來像這樣。配置文件:

//Set the host the databse will connect too 
    $config_db_hostname='xxx.xxx.xxx'; 
    //Set the user who is connecting to the database 
    $config_db_user='a user'; 
    //Set the password for user who is connecting to the database 
    $config_db_pass='abc123'; 
    //Set the database type 
    $config_db_type='mysql'; 
    //Set the name of the database 
    $config_db_name='phonephare_development'; 
    //Optional: Set the schema, if any 
    $config_db_schema=''; 
    //Optional: Set the port, otherwise default port will be used 
    $config_db_port=''; 
    //Set the prefix for the table names 
    $config_db_prefix='pf_'; 

    //Set the host the databse will connect too 
    $config_db_hostname_1='localhost'; 
    //Set the user who is connecting to the database 
    $config_db_user_1='test'; 
    //Set the password for user who is connecting to the database 
    $config_db_pass_1='test'; 
    //Set the database type 
    $config_db_type_1='mysql'; 
    //Set the name of the database 
    $config_db_name_1='test_development'; 
    //Optional: Set the schema, if any 
    $config_db_schema_1=''; 
    //Optional: Set the port, otherwise default port will be used 
    $config_db_port_1=''; 
    //Set the prefix for the table names 
    $config_db_prefix_1='pv_'; 

函數獲取值:

public static function init(){ 
     if(file_exists(PV_DB_CONFIG)){ 
      include(PV_DB_CONFIG); 


      for ($i = 0; $i <= 3; $i++) { 
       if($i){ 
        $profile_id='_'.$i; 
       } else { 
        $profile_id=''; 
       } 
       // Database variables 
       self::$connections['connection'.$profile_id]['dbhost'] = ($config_db_hostname.$profile_id); 
       self::$connections['connection'.$profile_id]['dbuser'] = $config_db_user.$profile_id; 
       self::$connections['connection'.$profile_id]['dbpass'] = $config_db_pass.$profile_id; 
       self::$connections['connection'.$profile_id]['dbtype'] = $config_db_type.$profile_id; 
       self::$connections['connection'.$profile_id]['dbname'] = $config_db_name.$profile_id; 
       self::$connections['connection'.$profile_id]['dbport'] = $config_db_port.$profile_id; 
       self::$connections['connection_'.$profile_id]['dbschema'] = $config_db_schema.$profile_id; 
       self::$connections['connection_'.$profile_id]['dbprefix'] = $config_db_prefix.$profile_id; 
      } 

     } 

}//end init 

所以,我怎麼能動態地看這個變量?

+6

男人,停止這樣做。改用數組! – GolezTrol

回答

4

這樣你可以閱讀「動態」的變量:

$varname = 'config_db_user'; 
if ($i) 
    $varname .= '_' . $i; 

$varvalue = $$varname; // $varvalue contains the value now. 

但是,這將使你的代碼真的難以閱讀,難以維持。而是讓你的配置是這樣的:

$profiles[1]['config_db_user'] = 'whatever'; 

然後,你可以閱讀$profiles[$profile_id]['configfield]得到你想要的東西。可能還有其他更好的方法,但不要把動態變量名放在一起,因爲這是最糟糕的。

1

以不同的方式構建您的數據。你想迭代低谷組變量共享一個相似的名字..但是,它沒有發生你的配置選項使用數組,並迭代低谷呢?

2

查詢PHP variable variables

注意:這是一個不好的配置文件格式,你應該把單獨的連接放到不同的數組中。檢查phpmyadmin的配置文件。