title: OO PHP protected properties not available in foreach loop?
body: <p>I have a validation class which I would like to use to check all values in my application are within allowed constraints.</p>
  
  <p>I am passing an object to a static function within the validation class, from another class (in this case User)</p>
  
  <pre><code>function validate() {
      $errors = Validation::validate($this);
  }
  </code></pre>
  
  <p>In the validation class, I create a new object and then proceed through the properties of the passed parameter object (or at least that is what I would like to do).</p>
  
  <pre><code>function validate($object) {
              $validation = new Validation();
              print_r($object);
              print_r('&lt;br /&gt;');
              foreach($object as $key =&gt; $val) {
                  print_r($val);
                  isset($val-&gt;maxlength) ? $validation-&gt;validateLengthNoMoreThan($val-&gt;value, $val-&gt;maxlength) : null;
                  isset($val-&gt;minlength) ? $validation-&gt;validateLengthAtLeast($val-&gt;value, $val-&gt;minlength) : null;
                  isset($val-&gt;required) &amp;&amp; ($val-&gt;required == true) ? $validation-&gt;validateNotBlank($val-&gt;value) : null;
              }
              return $validation-&gt;errors;
          }
  </code></pre>
  
  <p>I am printing out values within the function purely for test purposes.  What I don't understand is why the object prints out fine outside of the foreach loop, but if I try to access the values within the loop, nothing is displayed.</p>
  
  <p>This is what is displayed OUTSIDE of the foreach loop:</p>
  
  <pre>
  User Object ( 
  [username:protected] => Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 ) 
  [firstname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 )
  [lastname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 ) 
  ) 
  </pre>
  
  <p>The validation class does NOT extend the User class, so I understand why the values would not be available, just not why they are available outside of the loop but not inside of it.</p>
  
  <p>Also, what would be the best way to carry out validation on protected/private properties?</p>
  
  <p>Any advice/tips would be greatly appreciated.</p>
  
  <p>Thanks.</p>