dtkp51520 2014-08-25 13:40
浏览 86
已采纳

如何在php中使用类名作为case的switch case语句?

Is there a way to use the class names as cases in the folowing example? (pseudo code)

class Some_Class {

    static function get_image_size() {

        switch (get_class($image_size)) {

            case 'Class_1':
                        $image_size = 'related-post';
                        break;
                    }
            case 'Class_2':
                        $image_size = 'full';
                        break;
                    }                   

                        return $image_size;

                    }

}

I need this to call a specific variable form different php classes. Basically I need something like the following code but using switch statement:

class Some_class {

    static function get_image_size() {

        if Class_Name_1 {
            $image_size = 'related-post';
        }

        elseif Class_Name_2 {
            $image_size = 'full';
        }                   

        return $image_size;

    }

}

Thanks.

  • 写回答

1条回答 默认 最新

  • donglv7097 2014-08-25 13:57
    关注

    Ok first, if this "class name" thing is the name of a class where you call this function why not passing it as a variable to this static function?

    Second, if by class name you mean name of the "Some_Class", you might be able to access to this name through this code in a static function:

    $thisClassName = get_class(new self());
    

    !better:

    $thisClassName = get_class();
    

    or

    $thisClassName = __CLASS__;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分