Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88308 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96486 invoked from network); 18 Sep 2015 00:49:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Sep 2015 00:49:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=jbafford@zort.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=jbafford@zort.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zort.net designates 96.241.205.2 as permitted sender) X-PHP-List-Original-Sender: jbafford@zort.net X-Host-Fingerprint: 96.241.205.2 nova.zort.net Received: from [96.241.205.2] ([96.241.205.2:40542] helo=nova.zort.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 63/62-19961-4AF5BF55 for ; Thu, 17 Sep 2015 20:49:41 -0400 Received: from [10.0.1.2] (pulsar.zort.net [96.241.205.6]) (authenticated bits=0) by nova.zort.net (8.14.5/8.14.5) with ESMTP id t8I0nZYG027164 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 17 Sep 2015 20:49:35 -0400 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\)) In-Reply-To: Date: Thu, 17 Sep 2015 20:49:35 -0400 Cc: Rowan Collins , PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: <36B62BA7-3139-460B-A483-22F81A5F61D7@zort.net> References: <55FB4791.4040807@gmail.com> <55FB533F.7080301@gmail.com> To: Bob Weinand X-Mailer: Apple Mail (2.2104) Subject: Re: [PHP-DEV] Let's discuss enums! From: jbafford@zort.net (John Bafford) > On Sep 17, 2015, at 20:15, Bob Weinand wrote: >=20 >> Am 18.09.2015 um 01:56 schrieb Rowan Collins = : >>=20 >> On 18/09/2015 00:16, Marcio Almada wrote: >>> A kitten is working on thathttps://wiki.php.net/rfc/enum. Many = points >>> on the linked RFC are compatible to the points you raised so it = might >>> be worth reading before even starting any new proposal. >>=20 >> Aha, I hadn't seen that, should have thought to search the wiki. = Still, interesting that we seems to mostly agree on the key decisions, = although I've gone into more detail about things that he's left as = Future Scope, which is fair enough. >>=20 >> The main thing I'd change if I was writing it is the implicit = "ordinal" property; I don't really see the point of insisting that the = order I write "Red, Green, Blue" has some meaning. On the other hand, = I'd give much more prominence to the name, e.g. by making values() = return an associative array. You're much more likely to want to say "the = value whose name is 'RED'" than "the value I defined first in the list", = IMHO. >>=20 >> If you have some kind of initialiser syntax for the values - with or = without a constructor - you get to have ordinals or explicit values = (like the Flags example) if you want them, and just names if not: >>=20 >> enum RenewalAction{ >> Deny( 0 ), >> Approve( 1 ); >>=20 >> public $ordinal; >> } >> enum Flags{ >> a (1 << 0), >> b( 1 << 1), >> c( 1 << 2), >> d( 1 << 3 ); public $value; } >>=20 >> enum PimaryColours { RED, GREEN, BLUE } >>=20 >> Regards, >>=20 >> --=20 >> Rowan Collins >> [IMSoP] >=20 > The reason it is not an associative array is that the names are not = important. >=20 > But we actually need ordinal(), else we'll end up writing = array_search(TheEnum::values(), $value). > It's mainly important for the purpose of serializing/unserializing = (manually or internally). >=20 > You never should *rely* on the ordinal value of the enum for anything. = Enums are supposed to be value-less. It's a type. It's not a value. The = only usage of the value is going external. >=20 > Also, I honestly think the Flags example is a bad one. (At least as = long as we don't have a possibility to typehint combined enums...). If = you need that, use class constants. They provide exactly what you need. >=20 > Bob Besides providing serialization mapping (or, the equivalent of a = __toString() value if the enum is used in a strong context), an = important benefit to providing a value for enums would be to allow for = changing or deprecating enums. So you might want to say something like: enum FormEvents { PRE_SUBMIT, PRE_BIND =3D PRE_SUBMIT, //deprecated name, provide mapping to = new name } Another good reason for wanting to provide values is so that you could = write code like this: $eventHandlers =3D [ FormEvents::PRE_SUBMIT =3D> function() { =E2=80=A6 }, FormEvents::POST_SUBMIT =3D> function() { =E2=80=A6 }, ... ]; Although maybe the ideal solution here would be to allow for array = indexes to have more types than just int and string (so that the array = index actually is the enum, not some serialized representation). -John=