title: What kind of flags/permissions implementations should be?
body: <p>I have a problem. How should I implement permissions, that can be constantly added/changed ?</p>
  
  <p>Now I have this:</p>
  
  <ul>
  <li>my database table scheme has field <code>flags</code></li>
  <li>I can select what I need via, for example, <code>SELECT * from some_table WHERE flags&amp;'.FLAG_CLOSED|FLAG_ACTIVE.'='.FLAG_CLOSED|FLAG_ACTIVE;</code></li>
  <li>all flags are constants inside the code</li>
  </ul>
  
  <p>sample code, just for figuring out what I mean (it's not real code, it's even not tested):</p>
  
  <pre><code>class Sample {
  
      const FLAG_ACTIVE  = 0x01;
      const FLAG_CLOSED  = 0x02;
      const FLAG_DELETED = 0x04;
  
      private $db;
  
      public function __construct(...) {
         /* ... */
      }
  
      public function getClosed() {
          $flags = self::FLAG_ACTIVE | self::FLAG_CLOSED;
          $query = 'SELECT * FROM `test` WHERE `flags`&amp;'.$flags.'='.$flags;
          $stmt = $this-&gt;db-&gt;query($query);
          return $stmt-&gt;fetch(\PDO::FETCH_ASSOC);
      }
  }
  </code></pre>
  
  <p>It's easy to get filtered result via just one field compare, but it's so annoying to change it inside the code.</p>
  
  <p>Another way to that is to make additional table, that should have flag and value. But what will be more efficient? Should value have a bit-field-based value (i.e. 1, 2, 4, 8, .. etc) or something else, like 'CLOSED', 'ACTIVE' ?</p>
  
  <p>In this two cases table row will looks like:</p>
  
  <pre><code>id, description, flags
  ===============
  1, 'test', 0x12
  1, 'test', 'CLOSED,ACTIVE'
  </code></pre>
  
  <p>in the second case I need to filter by `WHERE flags like '%CLOSED%' and flags like '%ACTIVE%' ?</p>
  
  <p>Oh.. here is the another way to implements flags. To make a table that has an <code>Id</code> and <code>Flag</code>. Like:</p>
  
  <pre><code>Id,  flag
  ========
  1, CLOSED
  1, ACTIVE
  </code></pre>
  
  <p>may be here is exists more efficient implementation ?</p>
  
  <p>p.s. sry for inconvenience, it's first question, be patient</p>
  
  <p>p.p.s. I don't need to repeat this scheme exactly, I've just request an advice, how do that with more efficient way, it can be absolutely simple implementation without any bit-fields values.</p>