Why is my custom library failing, with a really weird error message?

Hi folks!

So I've put together this CodeIgniter library to encrypt stuff into Base62 and decrypt it back again.

Just before anyone asks, the file is application/libraries/Basecrypt.php

Here's the contents of Basecrypt.php:

<?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 > 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 < $len; ++$i) {
                $val += $arr[$str[$i]] * pow($base, $len-$i-1);
            return $val;
    /* End of file Basecrypt.php */
    /* Location: ./application/libraries/Basecrypt.php */

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.

Now, just to be 100% sure, I've loaded Basecrypt in both autoload, AND my Controller. Here's where I'm using it:

$id = $this->input->post('id');
    echo $this->basecrypt->encrypt($id);

Yep, $id is definitely set. But, nothing happens and I get this error message written to my PHP log:
Quote: 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

I thought, that's a bit fishy. CI_Form_validation?! I never mentioned that anywhere... did I?

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 `$this->awards->encode($id)` - and **that worked entirely fine**!

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 `$this->basecrypt->encode($id)` - 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.

Any ideas? Thanks, I really appreciate the help!


Must be something with the way you load it.

If I create the library using the code above, create a new controller with this in the index method:
$id = 7676424131321;

It produces
int 7676424131321

string '2b99XlKF' (length=8)

No errors whatsoever.

That's what I thought...:S

So, what's wrong with the way I'm loading it?

I'm calling it from a view file called from a controller, Code.php:

Class Code extends Controller {
    function __construct()

You can see Basecrypt is being loaded. Also, basecrypt is in the Autoload.php config file too...

My view file simply runs it as I described in the first post.

So - what's going wrong? Sad

[quote author="Jack W-H" date="1277864875"]Hi folks!

$id = $this->input->post('id');
echo $this->basecrypt->encrypt($id);


encrypt() or encode()?

Sorry, that really was a typo. I'm using encode() Smile

you are using
like an autoload array , but actually, from the userguide ( LOAD - LIBRARY )
$this->load->library('class_name', $config, 'object name');

so when you do this
you're saying, the instance of the 'form_validation' class will be created with 'yourls' params and will be named 'basecrypt'...

The proper way will be ( assuming that 'yourls' it's also a library )



Wow... I wish I'd known that from the start, as I've been doing it that way in my entire webapp... ?! If this is really the case, I have no idea how it's still functioning as expected with no other errors...

Thanks, vitoco - I'll give it a try and report back!


Sometimes it can be worth to RTFM... Smile

[eluser]GSV Sleeper Service[/eluser]
I have nothing to add to this other than "Hi Jack, from a fellow NMUG subscriber"

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.