title: Validating constructor parameters, making sure parameters have the correct type
body: <p>Writing my first PHP Class and came across an issue, the following is my <code>__construct</code> method:</p>
  
  <pre><code>public function __construct($foo,$bar) {
         $this-&gt;foo = $foo;
         $this-&gt;bar = $bar;
  }
  </code></pre>
  
  <p>Both <code>$foo</code> and <code>$bar</code> are required, without them, the methods will not work. It's OK when they aren't defined when the object is instantiated like so:</p>
  
  <pre><code>$var = new Class();
  </code></pre>
  
  <p>As this throws an exception (e.g. Class requires 2 params, none set). But if they are set but not of the correct type like so:</p>
  
  <pre><code>$var = new Class('33','ddd');
  </code></pre>
  
  <p>My methods will fail as the variables have the wrong type.</p>
  
  <p><strong>Where should I be validating these? In the constructor or in each method?</strong></p>
  
  <p>My solution that I'm using right now works, but I'm not sure if it's the right way:</p>
  
  <pre><code>// $foo needs to be a string with letters only
  // $bar needs to be an integer
  public function __construct($foo,$bar) {
         $this-&gt;foo = $foo;
         $this-&gt;bar = $bar;
         if(!is_numeric($bar)){
             // Throw exception
         }
         elseif(other validation case)
         etc...
  }
  </code></pre>
  
  <p>The concepts of OO programming are pretty new to me, so links to any reference material you have would be greatly appreciated.</p>