2017-06-03 13 views
0

我正在嘗試改進下面的switch語句。這裏發生的事情是,基於x個找到的令牌多次調用代碼,所以下面的代碼每個令牌運行一次。在PHP中改進switch語句或建議替代解決方法

如果找不到$post->ID,則會向該令牌發送通知,並將該ID添加到數據庫中。

但是在某些情況下,它可以在檢測到大約40%的令牌後停止,這大概是因爲找到了ID?由於我在wordpress上,我使用update_option將ID存儲在表中,但可能使用其他方法?

$os = $this->os; 
switch ($os) { 

    case "iOS": 
     $iOS_pastPushSavedID = get_option('iOS_pastPushSavedID', $default = false); 
     if($post->ID != $iOS_pastPushSavedID) { 
      update_option('iOS_pastPushSavedID', $post->ID, no); 
      $sendPush = true; 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Android": 
     $android_pastPushSavedID = get_option('android_pastPushSavedID', $default = false); 
     if($post->ID != $android_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $android_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('android_pastPushSavedID', $post->ID, no); 
      $sendPush = true;   
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $android_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Fire OS": 
     $fireos_pastPushSavedID = get_option('fireos_pastPushSavedID', $default = false); 
     if($post->ID != $fireos_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $fireos_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('fireos_pastPushSavedID', $post->ID, no); 
      $sendPush = true;   
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $fireos_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Safari": 
     $safari_pastPushSavedID = get_option('safari_pastPushSavedID', $default = false); 
     if($post->ID != $safari_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $safari_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('safari_pastPushSavedID', $post->ID, no); 
      $sendPush = true; 

     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $safari_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Chrome": 
     $chrome_pastPushSavedID = get_option('chrome_pastPushSavedID', $default = false); 
     if($post->ID != $chrome_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $chrome_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('chrome_pastPushSavedID', $post->ID, no); 
      $sendPush = true;   
     } else { 
      //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $chrome_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    case "Firefox": 
     $firefox_pastPushSavedID = get_option('firefox_pastPushSavedID', $default = false); 
     if($post->ID != $firefox_pastPushSavedID) { 
      //$title = ($os . '_New Push = ' . ' storedID: ' . $firefox_pastPushSavedID . '/postID: ' . $post->ID); 
      update_option('firefox_pastPushSavedID', $post->ID, no); 
      $sendPush = true; 

     } else { 
     //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $firefox_pastPushSavedID . '/postID: ' . $post->ID); 
      $sendPush = false; 
     } 
    break; 

    default: 
     $sendPush = false; 
} 

回答

1

除非我誤解你的過程,這是一個非常乾燥的/簡明的方式做到沒有詳細的switch/case塊:

$os_opts=[ 
    'iOS'=>'iOS', 
    'Android'=>'android', 
    'Fire OS'=>'fireos', 
    'Safari'=>'safari', 
    'Chrome'=>'chrome', 
    'Firefox'=>'firefox' 
]; 

$os=$this->os; 
$sendPush=false; 
if(isset($os_opts[$os])){       // deny empty and invalid options 
    $os_opt="{$os_opts[$os]}_pastPushSavedID";  // build string for next two functions 
    if($post->ID!=get_option($os_opt,$default=false)){ 
     update_option($os_opt,$post->ID,no); 
     $sendPush = true; 
    } 
} 

$os_opts陣列具有匹配$os的密鑰,以及與get_option() & update_option()一起使用的值。這將大大減少代碼長度,並使未來的修改非常容易。

由於get_option()結果只使用一次,所以將其聲明爲變量沒有意義;只要在if條件下使用它。

get_option()update_option()的第一個參數始終以相同的子字符串結尾。我有意義將$os_opts[$os]的值作爲前綴並將其聲明爲變量。變量聲明不是必需但我的個人規則是;如果您打算多次使用數據,請使用變量,如果只使用一次,則不要聲明它。

1

你可以這樣做。你可以像這樣縮短你的代碼。

$optionName='';//added some default values 
$sendPush = false;;//added some default values 
switch ($os) { 

    case "iOS": 
     $optionName='iOS_pastPushSavedID'; 
    break; 

    case "Android": 
     $optionName='android_pastPushSavedID'; 
    break; 

    case "Fire OS": 
     $optionName='fireos_pastPushSavedID'; 
    break; 

    case "Safari": 
     $optionName='safari_pastPushSavedID'; 
    break; 

    case "Chrome": 
     $optionName='chrome_pastPushSavedID'; 
    break; 

    case "Firefox": 
     $optionName='firefox_pastPushSavedID'; 
    break; 

    default: 
     $sendPush = false; 
} 
//this is operation which is common when $optionName is not empty. 
if(!empty($optionName)) 
{ 
    $optionData = get_option($optionName, $default = false); 
    if($post->ID != $optionData) { 
     update_option($optionData, $post->ID, no); 
     $sendPush = true; 
    } else { 
     $sendPush = false; 
    } 
} 
+0

謝謝!將檢查出來並恢復。感謝您的快速回復。你會建議一個更快的替代方法來將var存儲在MySQL Wordpress中嗎?或者我的解決方案好嗎? Tx – Jason

+0

@Jason歡迎...我認爲它是好的,這是你在這裏試圖做的事情的最短代碼。我的代碼,我給你一種方式,你可以防止自己重寫相同的代碼.. :) –

+0

謝謝!明天會試試這個,並會讓你知道! – Jason

1

編號寫更多像這樣

function getOptionSpecifier() { 

    switch ($this->os) { 
     case "iOS": 
      return 'iOS_pastPushSavedID'; 
     case "Android": 
      return 'android_pastPushSavedID'; 
     case "Android": 
      return 'android_pastPushSavedID'; 
     case "Fire OS": 
      return 'fireos_pastPushSavedID'; 
     case "Safari": 
      return 'safari_pastPushSavedID'; 
     case "Chrome": 
      return 'chrome_pastPushSavedID'; 
     case "Firefox": 
      return 'firefox_pastPushSavedID'; 
     default: 
      return ''; 

    } 
} 

function send_notification($id) { 
    $optionSpecifier = getOptionSpecifier(); 

    if ($optionSpecifier === NULL) { 
     return false; 
    } 

    $pastPushSavedID = get_option($optionSpecifier, $default = false); 

    if($id != $pastPushSavedID) { 
     update_option($optionSpecifier, $id, no); 
     return true; 
     //$title = ($os . '_New Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
    } else { 
     //$title = ($os . '_Duplicate Push = ' . ' storedID: ' . $iOS_pastPushSavedID . '/postID: ' . $post->ID); 
     return false; 
    } 
} 

$sendPush = send_notification($post->ID); 

ALA「關注點分離」多種功能等等......