首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改进PHP中的开关语句或建议另一种解决方法

改进PHP中的开关语句或建议另一种解决方法
EN

Stack Overflow用户
提问于 2017-06-03 16:58:45
回答 3查看 43关注 0票数 0

我正在努力改进下面的开关语句。这里发生的情况是,根据找到的令牌数量,代码被多次调用,所以下面的代码每个令牌运行一次。

如果找不到$post->ID,则会向该令牌发送通知,并将id添加到数据库中。

但是,在某个时候,在检查了大约40%的令牌之后,它就停止了,大概是因为找到了ID吗?因为我在wordpress上,所以我使用update_option将id存储在一个表中,但是也许可以使用另一种方法?

代码语言:javascript
复制
$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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-06-06 03:10:17

除非我误解了您的过程,否则这是一种非常枯燥/简洁的方法,无需使用冗长的switch/case块:

代码语言:javascript
复制
$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
EN

Stack Overflow用户

发布于 2017-06-03 17:06:44

你可以这样做。您可以这样缩短代码。

代码语言:javascript
复制
$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;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2017-06-03 17:20:04

我写得更像这样

代码语言:javascript
复制
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“分离关注点”所以..。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44346438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档