从类中获取枚举样式的值


Getting enum-style values from within a class?

我试图使用一个接口来代替PHP中缺乏枚举,但它似乎不像我想要的那样工作。

代码如下:

interface Brands
{
    const abrand = "A Brand";
    const anotherbrand = "Another Brand";
}
class Product
{
    private $brand;
    function __construct() {
    }
    public function getBrand() {
        return Brands::$this->brand;
    }
    public function setBrand($value) {
        $this->brand = $value;
    }
}
$product = new Product();
$product->setBrand("aproduct");
echo $product->getBrand();

谁能解释一下为什么输出是abrand而不是A Brand ?

好吧,那太蠢了。

我应该用$product->setBrand(Brands::abrand);而不是$product->setBrand("aproduct");

:/