Yes I think get that part, that's what I called 'class constants on steroids'. What I would do in PHP 7 is something like :
classCard{constDIAMONDS='diamonds';constHEARTS='hearts';constSPADES='spades';constCLUBS='clubs';constSUITS=[self::DIAMONDS,self::HEARTS,self::SPADES,self::CLUBS,];publicfunction__construct($suit){if(!in_array($suit,self::SUITS)){thrownewInvalidArgumentException('invalid suit');}}}//works$card=newCard(Card::SPADES);//works :$card=newCard('spades');//throws an exception :$card=newCard('hammers');
With Enums it becomes a lot easier as you said.
But I don't need to put methods on my enums for that ! If I want a method that returns the shape or color of a suit I'll just create a Suit interface with a abstracts shape() and color() method and implement that method in a Spade class.
In the RFC they have an example where the color method uses a match statement to return the color and always return 'rectangle' as a shape. To me this kind of defeats the purpose of OOP.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes I think get that part, that's what I called 'class constants on steroids'. What I would do in PHP 7 is something like :
With Enums it becomes a lot easier as you said.
But I don't need to put methods on my enums for that ! If I want a method that returns the shape or color of a suit I'll just create a
Suit
interface with a abstractsshape()
andcolor()
method and implement that method in aSpade
class.In the RFC they have an example where the color method uses a match statement to return the color and always return 'rectangle' as a shape. To me this kind of defeats the purpose of OOP.