2011-09-30 2 views
0

변수를 이름으로 가져 오는 데 문제가 있습니다. 내가하고있는 일은 연결이있는 경로를 포함하고 있으며 연결의 이름이 특정 방식으로 지정되기를 바랍니다. 이러한 회의를 반복하고 대기열에 추가하려고하지만 문제는 '사용할 수 없습니다.'입니다. 변수를 연결합니다.PHP 이름과 concatention으로 변수 얻기

코드는 다음과 같습니다. 구성 파일 :

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 

그래서 어떻게 동적으로 변수를 읽을 수 있습니다 값을 얻기위한

//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_'; 

기능?

+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의 설정 파일을 확인하십시오. 이 같은