title: Why is my custom CodeIgniter library failing with a rather confusing error message?
body: <p>So I've put together this CodeIgniter library to encrypt stuff into Base62 and decrypt it back again.</p>
  
  <p>Just before anyone asks, the file is application/libraries/Basecrypt.php</p>
  
  <p>Here's the contents of Basecrypt.php:</p>
  
  <pre><code>    &lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  
  class Basecrypt
  {       
      function encode($val) {
          // can't handle numbers larger than 2^31-1 = 2147483647
          $base = 62;
          $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
          $str = '';
          do {
              $i = $val % $base;
              $str = $chars[$i] . $str;
              $val = ($val - $i) / $base;
          } while($val &gt; 0);
  
          return $str;
      }
  
      function decode($str) {
          $base = 62;
          $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  
          $len = strlen($str);
          $val = 0;
          $arr = array_flip(str_split($chars));
          for($i = 0; $i &lt; $len; ++$i) {
              $val += $arr[$str[$i]] * pow($base, $len-$i-1);
          }
  
          return $val;
      }
  
  }
  
  /* End of file Basecrypt.php */
  /* Location: ./application/libraries/Basecrypt.php */
  </code></pre>
  
  <p>There's nothing particularly amazing about that. Just encodes and decodes. As far as I can tell, I've followed EVERY rule that CodeIgniter's manual specifies.</p>
  
  <p>Now, just to be 100% sure, I've loaded Basecrypt in both autoload, AND my Controller. Here's where I'm using it:</p>
  
  <pre><code>$id = $this-&gt;input-&gt;post('id');
  echo $this-&gt;basecrypt-&gt;encrypt($id);
  </code></pre>
  
  <p>Yep, $id is definitely set. But, nothing happens and I get this error message written to my PHP log:</p>
  
  <blockquote>
    <p>PHP Fatal error:  Call to undefined method CI_Form_validation::encrypt() in /Users/Jack/Sites/mysite.com/system/application/views/code/viewajax.php on line 4</p>
  </blockquote>
  
  <p>I thought, that's a bit fishy. CI_Form_validation?! I never mentioned that anywhere... did I?</p>
  
  <p>So as a final confirmation I put the two functions encode() and decode() into another custom library I was using (called Awards.php) and called it via <code>$this-&gt;awards-&gt;encode($id)</code> - and <strong>that worked entirely fine</strong>!</p>
  
  <p>To make matters even more confusing, I copy-pasted the entire Awards.php file into Basecrypt.php, only renaming the class declaration, and once again tried calling it through <code>$this-&gt;basecrypt-&gt;encode($id)</code> - however, that throws up the same error. Despite no differences between that version of Basecrypt and the version of Awards in which it worked perfectly fine. I've even tried renaming the functions and the class incase something was reserved, but it makes no difference.</p>
  
  <p>Any ideas? Thanks, I really appreciate the help!</p>
  
  <p>Jack</p>