> Am 18.09.2015 um 02:27 schrieb Ryan Pallas <[email protected]>:
>
> I few questions wrt the rfc: https://wiki.php.net/rfc/enum
>
>> An enum value is only equal to itself.
> I'm not sure I agree. How then do I store the enum into a DB and compare it
> after reading?
> switch($db->query('select role from user where user_id = 123')->fetch()[0])
> {
> case UserRole::Admin:
> include 'admin.php';
> break;
> case UserRole::User;
> include 'user.php';
> break;
> default:
> include 'login.php';
> break;
> }
> Would this not ALWAYS include login if UserRole is an enum (instead of a
> final class with constants as I use now, which a switch like this will work
> for)? Is this only possible with the magic value method? In which case I'd
> have to check the value method, something like case
> UserRole::Admin->ordinal() (or name, or value or whatever)?
You could use UserRole::values()[$ordinal] and then compare against that.
>> This means that if the name enum is used for a property, function,
> method, class, trait or interface there will now be a parse error instead.
> Surely, a property can be named enum, since it can be named other reserved
> words, for example?
> $ php
> <?php
> class Foo {
> public $function = 'callMe';
> public $trait = 'useMe';
> public $class = 'instantiateMe';
> }
> $f = new Foo();
> var_dump(get_object_vars($f));
>
> array(3) {
> 'function' =>
> string(6) "callMe"
> 'trait' =>
> string(5) "useMe"
> 'class' =>
> string(13) "instantiateMe"
> }
Obvious mistake in the RFC, I just changed it...
> Also, I really like the idea of being able to type hint the enum in the
> switch(UserRole) that was mentioned. Not sure I like the idea of an
> implicit throw though, consider the UserRole above, I might have 10
> different roles, but the current switch is only valid for 5 of those roles.
> In this situation, if I add a default: return Auth::NotAuthorized; will
> that supress the implicit fall throw? If so, how often will the implicit
> throw really be hit? I know we have standards that all case statements must
> have default statements, to make sure every case is handled, whether we
> foresaw it or not.
I'd really put that under Future Scope through. It's not really important to
the feature of Enums themselves and always can be added later on.
Bob
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php