2010-06-17 58 views

回答

1

那么,我自己的解决方案是针对不同的环境使用阵列中的不同键。

在这种情况下,我会尽力解释它在PHP

class API_Client 
{ 
    const ENV_STAGING = 'staging'; 
    const ENV_PRODUCTION = 'production'; 

    protected static $apiKeys = array(
     self::ENV_STAGING => 'thisisthekeyformystagingenv', 
     self::ENV_PRODUCTION => 'thisisthekeyformyproductionenv', 
    ); 

    protected static $environment = self::ENV_PRODUCTION; 

    public static function getEnvironment() 
    { 
     return self::$environment; 
    } 

    public static function setEnvironment($environment) 
    { 
     self::$environment = $environment; 
    } 

    public static function apiCall($call) 
    { 
     $environment = self::getEnvironment(); 
     if(array_key_exists(self::$apiKeys, $environment)) 
      $apiKey = self::$apiKeys[$environment]; 
     else throw new Exception("No API key found for current environment '$environment'"); 

     return self::_apiCall($apiKey, $call); 
    } 

    protected static function _apiCall($apiKey, $call) 
    { 
     // Make the call to the API 
    } 
} 

我希望这有助于...

相关问题