Re: [RFC] Enum proposal (yet another)
2012/2/22 Charlie Somerville <[email protected]>
> 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" <[email protected]> wrote:
>
> > On Wed, Feb 22, 2012 at 12:45 PM, Samuel Deal <[email protected]>
> > 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
> > > [email protected]
> >
> >
> >
> > --
> > Laruence Xinchen Hui
> > http://www.laruence.com/
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
Thread (70 messages)