title: Why does new $class; return null when class_exists($class) returns true?
body: <p>Sorry for the horrible question title, I wasn't sure how better to phrase it.</p>
  
  <p>I have a class loaded and can verify this with <code>error_log(json_encode(class_exists($class)));</code> where <code>$class</code> is a string that contains the class name, <code>"JFormRuleDate"</code> in this case.</p>
  
  <p>However when I call <code>$dateRule = new $class;</code> or <code>$dateRule = new $class();</code> then <code>$dateRule</code> gets set to <code>NULL</code>. Which I can verify with <code>error_log(json_encode($dateRule));</code></p>
  
  <p>The class inherits its constructor from its parent (which I can verify exists as above), and I could instantiate instances before I moved the file containing the class to a common library, but I didn't change the class so I'm not sure why it is behaving differently now.</p>
  
  <p>The class is autoloaded but the cases of the file and class are both correct, I've checked. using var_export (can't var_dump in this context) I get <code>JFormRuleUint::__set_state(array(\n   'regex' =&gt; '^[0-9]+$',\n   'modifiers' =&gt; NULL,\n))</code> which seems right, and <code>is_object($dateRule)</code> returns false, and trying to use <code>$dateRule</code> throws <code>Call to a member function test() on a non-object</code> errors. And <code>!$dateRule</code> also returns true.</p>
  
  <p>Never mind is_object returns false, I had that conditional backwards.</p>
  
  <p>So the simplest test case I could make is:</p>
  
  <pre><code>jimport('joomla.form.helper');
  $dateRule = JFormHelper::loadRuleType('date', true);
  $class ="JFormRuleDate";
  error_log(var_export(class_exists($class),true));
  error_log(var_export($dateRule,true));
  $date_rule = new $class;
  error_log(var_export($date_rule,true));
  if(!is_object($dateRule)){
        error_log('Couldn\'t load date validation rules.');
  }//else{//commented for demonstration purposes
        $rec_valid = $date_rule-&gt;test(array('required'=&gt;false), '10-2-2013');
  //}
  </code></pre>
  
  <p>And the class is defined as:</p>
  
  <pre><code>&lt;?php
  defined('JPATH_PLATFORM') or die;
  
  jimport('joomla.form.formrule');
  
  class JFormRuleDate extends JFormRule
  {
    protected $regex = '^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$';
    public function test(&amp;$element, $value, $group = null, &amp;$input = null, &amp;$form = null)
    {
      // If the field is empty and not required, the field is valid.
      $required = ($element['required'] === true || (string) $element['required'] === 'required');
      if (!$required &amp;&amp; empty($value)) {
        return true;
      }
  
      // Test the value against the regular expression.
      if (!parent::test($element, $value, $group, $input, $form)) {
        return false;
      }
  
      // bounds
      $parts = explode('-', $value);
      return checkdate($parts[1] , $parts[2], $parts[0]);
    }
  }
  ?&gt;
  </code></pre>
  
  <p>and my output is </p>
  
  <pre><code>[Fri Sep 06 16:06:08 2013] [error] [client ::1] true, referer: https://localhost//portals/admin-portal
  [Fri Sep 06 16:06:08 2013] [error] [client ::1] JFormRuleDate::__set_state(array(\n   'regex' =&gt; '^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$',\n   'modifiers' =&gt; NULL,\n)), referer: https://localhost/portals/admin-portal
  [Fri Sep 06 16:06:08 2013] [error] [client ::1] JFormRuleDate::__set_state(array(\n   'regex' =&gt; '^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$',\n   'modifiers' =&gt; NULL,\n)), referer: https://localhost/portals/admin-portal
  [Fri Sep 06 16:06:08 2013] [error] [client ::1] Couldn't load date validation rules., referer: https://localhost/portals/admin-portal
  [Fri Sep 06 16:06:08 2013] [error] [client ::1] PHP Fatal error:  Call to a member function test() on a non-object in /var/www/modules/mod_admin_form/helper.php on line 81, referer: https://localhost/portals/admin-portal
  </code></pre>
  
  <p>with <code>error_reporting(-1);</code> set, in addition to the above I also see:</p>
  
  <pre><code>[Fri Sep 06 16:13:56 2013] [error] [client ::1] PHP Notice:  Undefined variable: date_rule in /var/www/libraries/metabolistics/validation/validation.php on line 18, referer: https://localhost/portals/admin-portal
  </code></pre>
  
  <p>Just remembered ob_start and friends, the output of <code>var_dump($date_rule)</code> is <code>object(JFormRuleDate)#1068 (2) {\n  ["regex":protected]=&gt;\n  string(32) "^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$"\n  ["modifiers":protected]=&gt;\n  NULL\n}\n</code> and <code>var_dump(is_object($date_rule))</code> outputs <code>bool(true)</code>.</p>
  
  <p>So eventually I just wrapped the loading and instantiating in a separate class which holds the rules internally and provides them via getters. So I imagine there was some scope issue I was not understanding.</p>