盡管 enum 基于類(lèi)和對象,但它們不完全支持對象相關(guān)的所有功能。 尤其是枚舉條目不能有狀態(tài)。
以下對象功能可用,功能和其他對象一致:
TARGET_CLASS
包括枚舉自身。
目標過(guò)濾器 TARGET_CLASS_CONST
包括枚舉條目。
__CLASS__
和 __FUNCTION__
的功能和平時(shí)無(wú)差別
枚舉類(lèi)型的魔術(shù)常量 ::class
和對象完全一樣,
它是個(gè)包含命名空間的類(lèi)型名稱(chēng)。
由于枚舉條目是枚舉類(lèi)型的一個(gè)實(shí)例,因此它的 ::class
也和枚舉類(lèi)型一樣。
此外,不能用 new
直接實(shí)例化枚舉條目,
也不能用 ReflectionClass::newInstanceWithoutConstructor() 反射實(shí)例化。
這么做都會(huì )導致錯誤。
<?php
$clovers = new Suit();
// Error: Cannot instantiate enum Suit
$horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor()
// Error: Cannot instantiate enum Suit
?>