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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条