开发者

Why is my custom CodeIgniter library failing with a rather confusing error message?

开发者 https://www.devze.com 2023-01-05 08:04 出处:网络
So I\'ve put together this CodeIgniter library to encrypt stuff into Base62 and decrypt it back again.

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 开发者_如何学JAVA> 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:

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!

Jack


Make sure you are not automatically loading the form validation lib


The second line should be class BaseCrypt extends Library. That's why it's not working.


Problem solved!

Basically, the problem was that I was $this->load->ing the Basecrypt library incorrectly.

The code I was using was: $this->load->library('tank_auth','awards','basecrypt')

I incorrectly thought that you could load Libraries in array form, as you can do in various other instances. It turns out that loading libraries with that syntax in fact is saying that I'm going to load tank_auth, creating it with awards parameters, and naming it basecrypt...

So, to combat the problem, it was fixed like this:

$this->load->library('tank_auth');
$this->load->library('awards');
$this->load->library('basecrypt');

Simpler - yet, also considerably more complex - than I first thought!

Thanks for all your help everyone,

Jack

0

精彩评论

暂无评论...
验证码 换一张
取 消