On 2010-05-21 20:12:18 +0200, Stas Malyshev said:

Hi!

I agree that enum make more sense when it's a type but I disagree with
the fact that it make code less understandable. The goal of this patch
is to make it easy to create a list of constants when the value is not
necessarily important (For example when you create a lexer you don't
mind if the integer value of your token is 300 or 400 as soon as it's
a unique value and that you can use the name of this token to do your
comparison) and the only thing you want is to assign a unique value
within your enumeration.

Creating lexers isn't really a common thing for PHP (and shouldn't be). And you probably wouldn't produce the list of 400 tokens manually, at which point you could as well generate their values too. But if you have not that many - like 3-4 in your examples - I think it's better not to be lazy and put the values in.

Sure create lexer is not common in php.

In my case I create quite often some classes with list of options. The main goal
each time is to work with this options (constant) with bitwise operation. Like
the exemple bellow.

My point is that the RFC is wrong. Constant should not be incremented but should
be shifted left.
After the manner which is implemented don't affect me. Can be a " enum " keyword
or a stuff like " iota ".

<?php
// define somme access level
class Access {

   // starting to 13 or 14 options the risk to make a mistake is quite hight.
   const SECTION_1 = 1;
   const SECTION_2 = 2;
   const SECTION_3 = 4;
   const SECTION_4 = 8;
   const SECTION_5 = 16;
   const SECTION_6 = 32;
   const SECTION_7 = 64;
   const SECTION_8 = 128;
   const SECTION_9 = 256;
   const SECTION_10 = 512;
   const SECTION_11 = 1024;
   const SECTION_12 = 2048;
   const SECTION_13 = 4096;
   const SECTION_14 = 8192;
   const SECTION_15 = 16384;
   const SECTION_16 = 32768;
   // and other...

   public static function isAccess($access, $userAccess)
   {
       return (bool) $access & $userAccess;
   }
}

// define a user level
$userAccess = Access::SECTION_6 | Access::SECTION_15 | Access::SECTION_8;

// check the user level
if (Access::isAccess(Access::SECTION_2, $userAccess))
{
   // ... some code here
}
?>
--
Alban Leroux
s...@paradoxal.org
Web developper


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to