title: Why program show error with : array_key_exists()?
body: <p>I want to validate form register and I created class UserValidator and I try to check if the username field exists. In my opinion everything is correct, but show me 2 errors</p>
  <blockquote>
  <p>Warning: array_key_exists() expects parameter 2 to be array, null given in C:\xampp\htdocs\class\index.php on line 22
  Notice: username is not present in data in C:\xampp\htdocs\class\index.php on line 24.</p>
  </blockquote>
  <pre><code>&lt;?php
  
  if(isset($_POST['submit']))
  {
      $validation = new UserValidator($_POST);
      $errors =$validation-&gt;validateForm();
  }
  
  
  
  &lt;?php
  class UserValidator
  {
      private $data;
      private $errors =[];
      private static $fields =['username', 'email'];
  
  public function _construct($post_data)
  {
      $this-&gt;data = $post_data;
  }
  
  public function validateForm()
  {
      foreach(self::$fields as  $field)
      {
          if(!array_key_exists($field, $this-&gt;data))
          {
              trigger_error(&quot;$field is not present in data&quot;);
              return;
          }
      }
      $this-&gt;validateUsername();
      return $this-&gt;errors;
  }
  
  
  private function validateUsername()
  {
      $val = trim($this-&gt;data['username']);
      if(empty($val))
          $this-&gt;addError('username', 'username cannot be empty');
  else
  {
      if(!preg_match('/^[a-zA-Z-0-9]{6,12}$/', $val))
          {
          $this-&gt;addError('username', 'username must me 6-12 characters');
          }
  }
  
  
  }
  
  private function addError($key, $val)
  {
      $this-&gt;errors[$key] = $val;
  }
  
  }
  
  ?&gt;
  </code></pre>
  <pre class="lang-html prettyprint-override"><code> &lt;div id=&quot;register&quot;&gt;
       &lt;h1&gt;REJESTRACJA&lt;/h1&gt;
          &lt;form action=&quot;&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;&quot; method=&quot;post&quot;&gt;        
           &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col-25&quot;&gt;
          &lt;label for=&quot;loginRe&quot;&gt;Login&lt;/label&gt;
        &lt;/div&gt;
        &lt;div class=&quot;col-75&quot;&gt;
          &lt;input type=&quot;text&quot; name=&quot;username&quot; placeholder=&quot;Login...&quot;&gt;
          &lt;div class=&quot;error&quot;&gt;
          &lt;?php echo $errors['username'] ?? '' ?&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
          &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col-25&quot;&gt;
          &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
        &lt;/div&gt;
        &lt;div class=&quot;col-75&quot;&gt;
          &lt;input type=&quot;text&quot; name=&quot;email&quot; placeholder=&quot;Email...&quot;&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;button&quot;&gt;
              &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Utwórz konto&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;displayCenter&quot;&gt;
      &lt;div class=&quot;display&quot;&gt;
      &lt;/div&gt;
      &lt;/div&gt;
      &lt;/form&gt;
      &lt;/div&gt;
  </code></pre>