My point is that *although* it's certainly possible to do almost everything I 
want already, I should not have to write all that boilerplate for each enum.

I would much prefer to write something like 'enum { A, B, C }' rather than 
write a full class for the enum, duplicate each enum value multiple times (your 
example repeats 'foo' and 'bar' three times each), overload the __toString() 
magic method, do manual error handling in the constructor, etc.

Since PHP provides no way to overload the equality operator, any solution that 
tries to emulate enums as Samuel or I have written about will be flawed.  


On Wednesday, 22 February 2012 at 7:37 PM, Sebastian Krebs wrote:

> 2012/2/22 Charlie Somerville <char...@charliesomerville.com 
> (mailto:char...@charliesomerville.com)>
>  
> > Right, but enums could possibly be a lot richer than class constants are
> > now.
> >  
> > They could be a type where the only values are what's defined in the enum.
> > This could be used with type hinting:
> >  
> > enum Foo {
> > A,
> > B,
> > C
> > }
> >  
> > function bar(Foo $x) {
> > // ...
> > }
> >  
>  
>  
>  
> class MyEnum {
> const FOO = 'foo';
> const BAR = 'bar';
> private $value;
> public function __construct ($value) {
> if (!in_array($value, array(self::FOO, self::BAR)) throw new
> UnexpectedValueException;
> $this->value = $value;
> }
> public function __toString () { return $this->value; }
> }
>  
>  
> function doSomething (MyEnum $foo) { /* code */ }
>  
>  
> What I wanted to say: I don't see, what is not possible already?
>  
>  
>  
> >  
> > There'd be no need to do any manual checking that $x is a valid value.
> >  
> > Perhaps enum values could also be casted back to strings:
> >  
> > (string)Foo::B; // "B"
> >  
> > Sure, this is the kind of stuff that's possible in other ways already, but
> > first class support always helps.
> > On Feb 22, 2012 4:14 PM, "Laruence" <larue...@php.net 
> > (mailto:larue...@php.net)> wrote:
> >  
> > > On Wed, Feb 22, 2012 at 12:45 PM, Samuel Deal <samuel.d...@gmail.com 
> > > (mailto:samuel.d...@gmail.com)>
> > > wrote:
> > > > Hi all,
> > > >  
> > > > I really missed enums in PHP,
> > > Why? we have class constant.
> > >  
> > > thanks
> > > > So after reading I tryed to synthetise the various views.
> > > >  
> > > > You can find a draft here :
> > > > https://github.com/SamNrique/php-src/wiki/RFC-draft
> > > > (I can't write on the wiki, and perhaps it's better to finish the
> > > > discussion first)
> > > >  
> > > > There's an implementation with this draft.
> > > > I'd love to have feedbacks because it's my first php-core hack.
> > > >  
> > > > Thanks
> > > > --
> > > > Samuel Déal
> > > > samuel.d...@gmail.com (mailto:samuel.d...@gmail.com)
> > > >  
> > >  
> > >  
> > >  
> > >  
> > > --
> > > Laruence Xinchen Hui
> > > http://www.laruence.com/
> > >  
> > > --
> > > PHP Internals - PHP Runtime Development Mailing List
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >  
> >  
> >  
>  
>  
>  


Reply via email to