2010-06-17 54 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 
    } 
} 

我希望這有助於...

相關問題