dtkp51520 2014-08-25 05: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 05: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__;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部