<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Franco Traversaro</title>
    <description>The latest articles on DEV Community by Franco Traversaro (@belinde).</description>
    <link>https://dev.to/belinde</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F46554%2F82c1abe8-47cd-4b9c-8218-926aacf86b59.jpeg</url>
      <title>DEV Community: Franco Traversaro</title>
      <link>https://dev.to/belinde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/belinde"/>
    <language>en</language>
    <item>
      <title>Fantastic Passwords and how to generate them</title>
      <dc:creator>Franco Traversaro</dc:creator>
      <pubDate>Mon, 19 Nov 2018 10:55:09 +0000</pubDate>
      <link>https://dev.to/belinde/fantastic-passwords-and-how-to-generate-them-1f1</link>
      <guid>https://dev.to/belinde/fantastic-passwords-and-how-to-generate-them-1f1</guid>
      <description>&lt;p&gt;Password strength, password management, password memorization, password bla bla bla. We spend half a day writing (or saving in browser) password for any damned service we use online. And we always use the same passwords, because surely we know that is a bad (ugly!) habit, but we really can't memorize so many abstract &lt;em&gt;things&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;lot&lt;/em&gt; of articles has been written ont this argument. A &lt;em&gt;lot&lt;/em&gt; of good advices have been &lt;em&gt;adviced&lt;/em&gt;. XKCD has suggested, in my humble opinion, one of the best methods to generate a single, long, secure password:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4idr3q18650ijz2tigf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp4idr3q18650ijz2tigf.png" alt="XKCD has always the correct answer to the question you didn't were aware of" width="740" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But still, this is a good method to remember... one? three? six? different password. If you try to set a different password for every service you'll quickly have &lt;em&gt;a lot&lt;/em&gt; of possible permutations to remember: &lt;em&gt;«So, for gmail was correct horse battery staple, for github wolf cucumber pencil mug, then for slack is penguin banana honey bacon? Or it was honey wolf banana honey?»&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So, my personal solution is simple. Aren't we developers? Aren't we everyday managing algorithms? Aren't algorithms our daily tool to resolve problems? So, why don't we use an algorithm to generate a unique password for every site?&lt;/p&gt;

&lt;p&gt;The algorithm has to be simple, because we'll have to run in in our brain. For example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first three letters of the second level domain, in NATO phonetic alphabet, camel cased&lt;/li&gt;
&lt;li&gt;An open square bracket&lt;/li&gt;
&lt;li&gt;The length of second level domain, in letters&lt;/li&gt;
&lt;li&gt;The lenght of the first level domain, in number&lt;/li&gt;
&lt;li&gt;A square bracket closed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Obviously I've made up this algorithm for this post, so don't try it on my accounts! :-) By the way, it's complex but can be easily remembered, and generates different passwords for every site:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Site or service&lt;/th&gt;
&lt;th&gt;Generated password&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;dev.to&lt;/td&gt;
&lt;td&gt;DeltaEchoVictor[three2]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;gmail.com&lt;/td&gt;
&lt;td&gt;GolfMikeAlfa[five3]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;github.com&lt;/td&gt;
&lt;td&gt;GolfIndiaTango[six3]&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And so on. Obviously everyone can improve his algorithm as he wish: adding other symbols depending on any parameter, inverting the order of characters, adding a simple Caesar encryption... There are no limits, just the mental complexity everyone decide is affordable.&lt;/p&gt;

&lt;p&gt;For the most paranoid ones (and when we talk about security we surely &lt;em&gt;should&lt;/em&gt; be all paranoid), the algorithm should be quite robust also if a single password gets violated. Il some bad hackers should violate github and in some way get our password, a human being reading &lt;code&gt;GolfIndiaTango[six3]&lt;/code&gt; could understand the phonetical part, and think about the possibility of an algorithm approach, and try it on other services we use. I think it's a remote possibility, because it can happen only if someone is targetting exactly &lt;em&gt;you&lt;/em&gt;, and normally this isn't the case. But still is a possibility, so every one must choose his algorithm carefully.&lt;/p&gt;

&lt;p&gt;I've never read anything about this algorithmic approach, but I've not searched about either, so as we say in Italy maybe "I've discovered the hot water". But I really love reading your considerations about.&lt;/p&gt;

</description>
      <category>security</category>
      <category>password</category>
    </item>
    <item>
      <title>Private parts exposed in public</title>
      <dc:creator>Franco Traversaro</dc:creator>
      <pubDate>Wed, 08 Aug 2018 17:36:19 +0000</pubDate>
      <link>https://dev.to/belinde/private-parts-exposed-in-public-fl2</link>
      <guid>https://dev.to/belinde/private-parts-exposed-in-public-fl2</guid>
      <description>&lt;p&gt;It's said that a good object keeps private or protected his properties, so nobody can harm the business logic. It's a wise advice, indeed, but sometimes we &lt;em&gt;want&lt;/em&gt; to break things: maybe we need to gain access of a private connection managed by an external library, maybe we just want to see the world burn. An example of the first case happened to me a lot of years ago, when I was working on a strange Wordpress integration: I needed the underlying &lt;code&gt;mysqli&lt;/code&gt; connection, but it was stored in a &lt;code&gt;dbh&lt;/code&gt; property of the &lt;code&gt;$wpdb&lt;/code&gt; singleton of class &lt;code&gt;\wpdb&lt;/code&gt;. I couldn't edit the class to make &lt;code&gt;dbh&lt;/code&gt; property public, but I needed the connection. What to do? We all know that a private or protected properties cannot be accessed from outside the instance through &lt;code&gt;-&amp;gt;&lt;/code&gt; operator! A &lt;code&gt;$object-&amp;gt;privateProp&lt;/code&gt; will &lt;em&gt;always&lt;/em&gt; fail!&lt;/p&gt;

&lt;p&gt;That's not entirely true. Quite, but not entirely. There's a little detail that I think it's not so known: the access control to methods and properties is done checking the context from which we are working. So, if we are &lt;em&gt;inside&lt;/em&gt; a method of the class, we can access private or protected properties of &lt;em&gt;any&lt;/em&gt; instance of that class.&lt;/p&gt;

&lt;p&gt;Enough talk, let's do some code. Imagine that we have a third-party library, that we cannot modify, that do this kind of things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// begin read only code&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeepThought&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$theAnswer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nv"&gt;$theQuestion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'How many roads...'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$singleton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DeepThought&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// end read only code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We disperately want to know &lt;code&gt;theAnswer&lt;/code&gt;: we know that is protected, so we can work on it. We know that a protected property can be accessed from extended classes, so we write this piece of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Towel&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;DeepThought&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getTheAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;DeepThought&lt;/span&gt; &lt;span class="nv"&gt;$instance&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$instance&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;theAnswer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nc"&gt;Towel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;getTheAnswer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$singleton&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you see the magic? &lt;code&gt;Towel&lt;/code&gt; extends &lt;code&gt;DeepThought&lt;/code&gt;, so can legitimately access his protected properties. When we call &lt;code&gt;Towel::getTheAnswer()&lt;/code&gt; we are in the &lt;code&gt;Towel&lt;/code&gt; context, so &lt;code&gt;$instance-&amp;gt;theAnswer&lt;/code&gt; is also legit.&lt;/p&gt;

&lt;p&gt;But &lt;code&gt;theAnswer&lt;/code&gt; is merely protected, and we also want to know &lt;code&gt;theQuestion&lt;/code&gt;, that's private. Extending the class we still have not access to private properties... we need a bigger towel! :-)&lt;/p&gt;

&lt;p&gt;Reflection comes to the rescue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$refl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ReflectionProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DeepThought&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'theQuestion'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$refl&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setAccessible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$refl&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$singleton&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that Reflection works also on protected properties, but I've presented it last for the sake of narration. We just instantiate a ReflectionProperty for &lt;code&gt;theQuestion&lt;/code&gt;: that's like a tool that can inspect or edit some attributes without changing the code. After we use the reflection to make the property accessible, and lastly we apply our edited property description to the original object. It's like &lt;code&gt;.call()&lt;/code&gt; in JavaScript.&lt;/p&gt;

&lt;p&gt;That's all! With these two techniques you can tear apart any class to extract the gems inside. But stay wise! Objects with private properties often have good reasons to be designed that way. No developer take care of backward compatibility for private properties: as in real life, if you play with other's private parts without consent you can get in trouble!&lt;/p&gt;

</description>
      <category>php</category>
      <category>reflection</category>
    </item>
    <item>
      <title>Catch the flag!</title>
      <dc:creator>Franco Traversaro</dc:creator>
      <pubDate>Sat, 30 Jun 2018 15:04:09 +0000</pubDate>
      <link>https://dev.to/belinde/catch-the-flag-3a11</link>
      <guid>https://dev.to/belinde/catch-the-flag-3a11</guid>
      <description>&lt;p&gt;Have you ever seen configurations written as a concatenation of constants? Something like &lt;code&gt;error_reporting = E_ERROR|E_CORE_ERROR&lt;/code&gt;: what kind of sorcery is this? It turns out this is a real common (and smart) way to pack a lot of boolean flags into a single value.&lt;/p&gt;

&lt;p&gt;So let's make something that use binary flags. I'll use PHP, but the concepts are applicable to any language with support of boolean bitwise operators. They're quite common, you'll admit. So, let's assume that we have a function that set the table when we invite our friends home for dinner. Sometimes we do a very special dinner with appetizer, first course, second course and dessert (I'm from Italy, so it's not uncommon), sometimes just a first course and dessert, sometimes any other permutation. So we do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nv"&gt;$appetizer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nv"&gt;$firstCourse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nv"&gt;$secondCourse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nv"&gt;$dessert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$appetizer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$firstCourse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// here the others checks&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, that's &lt;em&gt;really ugly&lt;/em&gt;. If we need just dessert we must call an embarassing &lt;code&gt;setTheTable(false, false, false, true)&lt;/code&gt;. If we need to add support for a welcome drink AND coffee AND liqueur (Italy, remember?) we must add 3 others variables, all of them enqueued to the previous ones because we want backward compatibility... Really, a terrible solution. It could be better if we use a configuration structure, in PHP arrays are the best solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'appetizer'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'firstCourse'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// here the others checks&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's better, but still kinda sucks. Now we depend on a bunch of custom strings: that's ok if the function is the only one that use them, but if some other parts of the project depends on them you can see the problems: string mispelled can bring bugs quite difficult to spot, and the IDE cannot help you. That's the work for constants, or enums, or other similar language constructs, but if we want to use an object instead of an array we could have problems using them. What a mess! We want just something really simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* some magic here for appetizer */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* some magic here for first course */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// here the others checks&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But how can we achieve this?&lt;/p&gt;

&lt;p&gt;The most important information you have to know is that a decimal number can be written in binary form. Here's a quick reference with the numbers we'll use in this article; I'll put also a decomposition, later you'll find why:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Decimal&lt;/th&gt;
&lt;th&gt;Binary&lt;/th&gt;
&lt;th&gt;Decomposition&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;2 + 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0100&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0101&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4 + 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0110&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4 + 2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0111&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;4 + 2 + 1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Stick your attention to those number with no decomposition: 1, 2, 4 and 8. They are all powers of 2, and in their binary form we can find just one 1 and all zeroes. Keep in mind this detail: powers of 2 are &lt;em&gt;important&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;And now for something completely different whe must analyze what bitwise operators do on binary numbers, because they'll be the key of our flag system. We normally use boolean operators, like &lt;code&gt;if ($a AND $b)&lt;/code&gt;, that consider the operands as a whole boolean value: so we know that &lt;code&gt;true AND false&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;true OR false&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;, &lt;code&gt;true XOR true&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;NOT true&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Bitwise operators do the same thing, but at a lower level, bit by bit. The notation is also different. C language has taught us to write &lt;code&gt;&amp;amp;&lt;/code&gt; for bitwise &lt;code&gt;AND&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt; for &lt;code&gt;OR&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt; for &lt;code&gt;XOR&lt;/code&gt; and &lt;code&gt;~&lt;/code&gt; for &lt;code&gt;NOT&lt;/code&gt;. So we can try a simple &lt;code&gt;0001 | 0010&lt;/code&gt;: we'll write in column, in order to make clear the idea:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0001&lt;/code&gt; → 1&lt;br&gt;
&lt;code&gt;0010&lt;/code&gt; → 2&lt;br&gt;
&lt;code&gt;0011&lt;/code&gt; → 1|2 = 3&lt;/p&gt;

&lt;p&gt;Do you see the logic? The operator has checked the first character of the two operands and applied a normal boolean logic: 0 is &lt;code&gt;false&lt;/code&gt;, 1 is &lt;code&gt;true&lt;/code&gt;, so &lt;code&gt;false OR false&lt;/code&gt; gets &lt;code&gt;false&lt;/code&gt;, a.k.a. 0; the second character is the same; the thirdth is &lt;code&gt;false OR true&lt;/code&gt;, so &lt;code&gt;true&lt;/code&gt;, a.k.a. 1; the fourth is &lt;code&gt;true OR false&lt;/code&gt;, so another 1. Pretty simple, isn't it?&lt;/p&gt;

&lt;p&gt;Maybe now you'll say "Ok, bitwise OR is an addition, because 1|2=3". Well, definitely &lt;em&gt;no&lt;/em&gt;. Try &lt;code&gt;1|1&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0001&lt;/code&gt; → 1&lt;br&gt;
&lt;code&gt;0001&lt;/code&gt; → 1&lt;br&gt;
&lt;code&gt;0001&lt;/code&gt; → 1|1 = 1&lt;/p&gt;

&lt;p&gt;So, bitwise operators sticks to just one character at a time: the "addition" has not a "carry". Let's have another example, with bitwise AND:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0111&lt;/code&gt; → 7&lt;br&gt;
&lt;code&gt;0101&lt;/code&gt; → 5&lt;br&gt;
&lt;code&gt;0101&lt;/code&gt; → 7&amp;amp;5 = 5&lt;/p&gt;

&lt;p&gt;That's interesting: all the 1 in &lt;code&gt;0101&lt;/code&gt; are paired with a 1 in &lt;code&gt;0111&lt;/code&gt;, so the result is another &lt;code&gt;0101&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We know (or you'll know in a few seconds) that 5 is deconstructed in 4 + 1, that is &lt;code&gt;0100 + 0001&lt;/code&gt;, because in binary numbers each character "gain" a value based on his position, in powers of 2, from right to left; we can write &lt;code&gt;0101&lt;/code&gt; as &lt;code&gt;0*8 + 1*4 + 0*2 + 1*1&lt;/code&gt;, and if you strip off the 0 multiplication and simplify the *1, you'll get just &lt;code&gt;4+1&lt;/code&gt;. Pretty simple, isn't it? So we can say that "destructuring" is like an inversed bitwise OR, because &lt;code&gt;1|4&lt;/code&gt; is equal to &lt;code&gt;0001 | 0100&lt;/code&gt;, that is &lt;code&gt;0101&lt;/code&gt;, our 5.&lt;/p&gt;

&lt;p&gt;Now, take a deep breath. Try to call 1, 2, 4, and 8 with other names, like appetizer, firstCourse, secondCourse and dessert. So 5, &lt;code&gt;0101&lt;/code&gt;, is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 dessert (0*8)&lt;/li&gt;
&lt;li&gt;1 secondCourse (1*4)&lt;/li&gt;
&lt;li&gt;0 firstCourse (0*2)&lt;/li&gt;
&lt;li&gt;1 appetizer (1*1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They seems like flags, am I right? So we can try this approach (but remember, the values must be all non-zero differents powers of two):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Constants declaration&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'APPETIZER'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'FIRST_COURSE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'SECOND_COURSE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'DESSERT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Our beloved function&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// here the checks&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Our business logic&lt;/span&gt;
&lt;span class="nf"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;FIRST_COURSE&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SECOND_COURSE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;FIRST_COURSE&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;APPETIZER&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;SECOND_COURSE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's good! Now we have constants that our IDE can recognize, the order of parameters is ininfluent, we can add whatever other value without breaking compatibility. We have just one limitation: we're using integers, so we have 32 or 64 bits available (the limits depend on language, architecture, seasonal weather, astral conjunctions and so on; RTFM is the only way to know the real limit). But if you're thinking about a function with more than 32 boolean switches you have more than a problem.&lt;/p&gt;

&lt;p&gt;But... how can we check for flags? For example, the recurring 5, that we write as &lt;code&gt;APPETIZER|SECOND_COURSE&lt;/code&gt;: we need to check if the &lt;code&gt;APPETIZER&lt;/code&gt; is enabled or not. In other words, check if in the deconstruction of the configuration parameter we have the value for &lt;code&gt;APPETIZER&lt;/code&gt;. That's a bitwise AND operation:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0101&lt;/code&gt; → 5 → &lt;code&gt;APPETIZER|SECOND_COURSE&lt;/code&gt;&lt;br&gt;
&lt;code&gt;0001&lt;/code&gt; → 1 → &lt;code&gt;APPETIZER&lt;/code&gt;&lt;br&gt;
&lt;code&gt;0001&lt;/code&gt; → 5&amp;amp;1 = 1 → &lt;code&gt;APPETIZER&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And if we check for &lt;code&gt;DESSERT&lt;/code&gt;? Let's see:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0101&lt;/code&gt; → 5 → &lt;code&gt;APPETIZER|SECOND_COURSE&lt;/code&gt;&lt;br&gt;
&lt;code&gt;1000&lt;/code&gt; → 8 → &lt;code&gt;DESSERT&lt;/code&gt;&lt;br&gt;
&lt;code&gt;0000&lt;/code&gt; → 5&amp;amp;8 = 0 → we don't have a constant for that, so it's simply a &lt;code&gt;false&lt;/code&gt; value&lt;/p&gt;

&lt;p&gt;Cool! Now we can fill those "some magic here" comments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;APPETIZER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;FIRST_COURSE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// here the others checks&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that's pretty! Simple and concise. And now we can do some cool trick, like check any flag from a selection: we could check if we have appetizer or dessert or both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;APPETIZER&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does it works? First we calculate &lt;code&gt;APPETIZER|DESSERT&lt;/code&gt;: &lt;code&gt;0001|1000 = 1001&lt;/code&gt;, and then we check if &lt;code&gt;$conf&lt;/code&gt; has some 1 in common; let's see some examples with different configurations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Literal $conf&lt;/th&gt;
&lt;th&gt;$conf&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;th&gt;Literal result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;APPETIZER&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;APPETIZER&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;APPETIZER⎮FIRST_COURSE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0011&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;APPETIZER&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;APPETIZER⎮DESSERT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;1001&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;APPETIZER⎮DESSERT&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;FIRST_COURSE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0010&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;–&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;FIRST_COURSE⎮SECOND_COURSE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0110&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;–&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So the result carry the information about which flag was effectively on. So we can also check that ALL the required flags are active:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="no"&gt;APPETIZER&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;APPETIZER&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a little verbose, but if you do this check just once it's ok. But what if you do a lot of dinner with  just first course and dessert, and a lot of complete dinner with all the components? Well, you can define derived constant!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Basic constants declaration&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'APPETIZER'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'FIRST_COURSE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'SECOND_COURSE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'DESSERT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Derived constant declaration&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'SMALL_DINNER'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="no"&gt;FIRST_COURSE&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'FULL_DINNER'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="no"&gt;APPETIZER&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;FIRST_COURSE&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;SECOND_COURSE&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// We can use derived constant like any other:&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;SMALL_DINNER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// we have a first course, or a dessert, or both&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SMALL_DINNER&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$conf&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;SMALL_DINNER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// we surely have a first course AND a dessert&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's also convenient because now you can play with negations! Let's see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;setTheTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="no"&gt;FULL_DINNER&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="no"&gt;DESSERT&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does it works? First let's see what's the value of &lt;code&gt;FULL_DINNER&lt;/code&gt;: it's defined as &lt;code&gt;APPETIZER|FIRST_COURSE|SECOND_COURSE|DESSERT&lt;/code&gt;, so it's &lt;code&gt;0001|0010|0100|1000 = 1111&lt;/code&gt;. After we do &lt;code&gt;~DESSERT&lt;/code&gt;, and it's &lt;code&gt;~1000 = 0111&lt;/code&gt;. The last step is the AND of those two values: &lt;code&gt;1111&amp;amp;0111 = 0111&lt;/code&gt;. We have effectively removed the dessert from the configuration, what a shame. Now, for just 4 flags it's not so useful, but try to think with a lot of flags, like in PHP, the configuration of error reporting: &lt;code&gt;E_ALL &amp;amp; ~E_NOTICE&lt;/code&gt; is waaaaay better than listing &lt;em&gt;all&lt;/em&gt; the other available levels. And if another level will be added in the remote future, the developers will take care of changing the real value of &lt;code&gt;E_ALL&lt;/code&gt;, so your configuration will remain valid. This brings an important suggestion: never, never, &lt;em&gt;never&lt;/em&gt; use directly the numerical values, use only the literal constants; numbers can change their meanings, on the other hand constants are, well, &lt;em&gt;constants&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;Just a last trick: if you prefer, you can write your constants directly in binary integer notation (if your language of choice supports it). When you'll get used with binary flags it will be useless, but at the beginning can make concepts clearer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'APPETIZER'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="mb"&gt;0b0001&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'FIRST_COURSE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mb"&gt;0b0010&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'SECOND_COURSE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mb"&gt;0b0100&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;span class="nb"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;'DESSERT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="mb"&gt;0b1000&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;This was just an introduction to binary flags; there are some interesting trick also while editing those values, but I'll think that for this article &lt;em&gt;ça suffit&lt;/em&gt;. If these concepts will be usefull please let me know, this was my first article on dev.to and I'm craving for feedback!&lt;/p&gt;

</description>
      <category>coding</category>
      <category>tutorial</category>
      <category>tips</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
