PHP const数组访问

时间:2012-08-08 04:58:20

标签: php arrays enums const associative-array

我试图理解如何使用PHP中的关联数组和扩展来构建一种知识树/本体。以下示例显示了我正在尝试执行的操作:

class Fruit {
    public static $TYPES = array("APPLE" => array("GREEN" => Apple::GREEN), array("RED" => Apple::RED)); 
}

class Apple {
    public static $GREEN = array("GRANNY_SMITH" => array("FLAVOUR" => array(Flavour::SHARP, Flavour::SWEET)), 
                                 "GOLDEN_DELICIOUS" => array("FLAVOUR" => array(Flavour::SWEET, Flavour::BLAND))); 

    public static $RED = array("RED_DELICIOUS" => array("FLAVOUR" => array(Flavour::SOUR, Flavour::SHARP)), 
                               "PAULA_RED" => array("FLAVOUR" => array(Flavour::SWEET, Flavour::SOUR)));  
}

class Flavour {
    const SHARP = "sharp";
    const SWEET = "sweet";
    const SOUR = "sour";
    const BLAND = "bland";
}

从此我希望能够检索如下值:

$flavours = Fruit::TYPES["APPLE"]["GREEN"]["GOLDEN_DELICIOUS"];

所以基本上我得到了与Golden Delicious苹果相关的风味列表...有没有更好的方法来表示PHP中的静态数据树?在Java中我会使用Enums ...

2 个答案:

答案 0 :(得分:3)

回答有关不可变数组的注释:

class ImmutableArray extends ArrayObject
{
    const ERROR = 'Immutable array!';

    public function __construct($input, $flags = 0, $iterator_class = 'ArrayIterator') {
        parent::__construct($input, $flags, $iterator_class);
    }

    public function __get($key) {
        return $this->offsetGet($key);
    }

    public function __isset($key) {
        return $this->offsetExists($key);
    }

    public function __set($key, $value) {
        throw new Exception(self::ERROR);
    }

    public function __unset($key) {
        throw new Exception(self::ERROR);
    }

    public function append($value) {
        throw new Exception(self::ERROR);
    }

    public function asort() {
        throw new Exception(self::ERROR);
    }

    public function ksort() {
        throw new Exception(self::ERROR);
    }

    public function natcasesort() {
        throw new Exception(self::ERROR);
    }

    public function natsort() {
        throw new Exception(self::ERROR);
    }

    public function offsetSet($key, $value) {
        throw new Exception(self::ERROR);
    }

    public function offsetUnset($key) {
        throw new Exception(self::ERROR);
    }

    public function uasort($cmp_function) {
        throw new Exception(self::ERROR);
    }

    public function uksort($cmp_function) {
        throw new Exception(self::ERROR);
    }
}

答案 1 :(得分:0)

对于任何寻找现代PHP版本答案的人:因为php 5.6 const数组和复杂的初始化是用PHP实现的。见what's new in php 5.6

class Fruit {
    const TYPES = [
        "APPLE" => ["GREEN" => Apple::GREEN, "RED" => Apple::RED]
    ]; 
}

class Apple {
    const GREEN = [
        "GRANNY_SMITH" => ["FLAVOUR" => [Flavour::SHARP, Flavour::SWEET]], 
        "GOLDEN_DELICIOUS" => ["FLAVOUR" => [Flavour::SWEET, Flavour::BLAND]]
    ]; 

    const RED = [
        "RED_DELICIOUS" => ["FLAVOUR" => [Flavour::SOUR, Flavour::SHARP]], 
        "PAULA_RED" => ["FLAVOUR" => [Flavour::SWEET, Flavour::SOUR]]
    ];  
}

class Flavour {
    const SHARP = "sharp";
    const SWEET = "sweet";
    const SOUR = "sour";
    const BLAND = "bland";
}

var_dump(Fruit::TYPES["APPLE"]["GREEN"]["GOLDEN_DELICIOUS"]);

上面的代码将输出(如预期的那样):

  

array(1){'FLAVOR'=> array(2){[0] => string(5)“sweet”1 =>   string(5)“bland”}}