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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?