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


How can use switch case statement using class names as case in php?

是否有办法在下面的例子中使用类名作为大小写?(伪代码)

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;
                    }
}

我需要这个调用不同的php类的特定变量。基本上,我需要像下面这样的代码,但使用switch语句:

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;
    }
}

谢谢。

首先,如果这个"类名"是你调用这个函数的类名为什么不把它作为变量传递给这个静态函数呢?

其次,如果类名指的是"Some_Class"的名称,那么可以通过静态函数中的以下代码访问该名称:
$thisClassName = get_class(new self());

!好:

$thisClassName = get_class();

$thisClassName = __CLASS__;