我试图了解如何使用 PHP 中的关联数组和 const 构建一种知识树/本体。以下示例显示了我正在尝试做的事情:
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"];
所以基本上我得到了一个与金冠苹果相关的口味列表......有没有更好的方法来表示像这样在 PHP 中的静态数据树?在Java中我会使用枚举...