CodeIgniter Forums
non object issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: non object issue (/showthread.php?tid=26480)

Pages: 1 2


non object issue - El Forum - 01-14-2010

[eluser]megabyte[/eluser]
Here's my code, Ican't see a need for more than this part of it:

Code:
function math_captcha($str, $field)
    {
        
        $this->load->library('math_captcha');
        
        $this->form_validation->set_message('math_captcha',  
            'The "Are you human" answer is incorrect.');  
        
        if ( ! isset($_POST[$field]))
        {
            return FALSE;                
        }
        
        $field = $_POST[$field];
                
        $field = $this->math_captcha->decrypt_answer($field);

        return ($str !== $field) ? FALSE : TRUE;
    }

I'm getting the error for this line

Code:
$field = $this->math_captcha->decrypt_answer($field);

Fatal error: Call to a member function on a non-object

Here's the library


Code:
class Math_captcha {

    /**
     * component settings
     *
     * @access public
     * @var array
     */
    var $key = 'apple';
    
    /**
     * component settings
     *
     * @access public
     * @var array
     */
    var $math_captcha_answer = '';
    
    
    /**
     * component settings
     *
     * @access public
     * @var array
     */
    var $settings = array();
    
    /**
     * Default values for settings.
     *
     * @access private
     * @var array
     */
    var $_defaults = array(
        'operand' => '+',
        'minNumber' => 1,
        'maxNumber' => 5,
        'numberOfVariables' => 2
    );
    
    /**
     * The variables used in the equation.
     *
     * @access public
     * @var array
     */
    var $variables = array();
    
    /*
     * The math equation.
     *
     * @access public
     * @var string
     */
    var $math_captcha_equation = null;
    
    
    
    
    function Math_captcha($settings = array())
    {
        $this->settings = $settings;
        
        $this->_init();
    }
    
    
    
    function _init()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('encrypt');
        $this->settings = array_merge($this->_defaults, $this->settings);
        
        $this->generate_equation();
        
        $this->generate_answer();
    }
    
    
    /*
     * Method that generates a math equation based on the component settings. It also calls
     * a secondary function, registerAnswer(), which determines the answer to the equation
     * and sets it as a session variable.
     *
     * @access public
     * @return string
     *
     */
    function generate_equation() {
        // Loop through our range of variables and set a random number for each one.
        foreach (range(1, $this->settings['numberOfVariables']) as $variable) {
            $this->variables[] = rand($this->settings['minNumber'], $this->settings['maxNumber']);
        }
        
       // debug($this->settings); debug($this->variables);
        $this->math_captcha_equation = implode(' ' . $this->settings['operand'] . ' ', $this->variables);
        //debug($this->equation);
        // This function determines the answer to the equation and stores it as a session variable.
    }
    
    /*
     * Determines the answer to the math question from the variables set in generateEquation()
     * and encryt it
     *
     * @access public
     * @return integer
     */
    function generate_answer() {
        // The eval() function gives us the $answer variable.
        eval("\$answer = ".$this->math_captcha_equation.";");
        $this->math_captcha_answer =  $this->encrypt_answer($answer);
    }
    
    
    /*
     * Compares the given data to the registered equation answer.
     *
     * @access public
     * @return boolean
     */
    function encrypt_answer($answer) {
        return $this->CI->encrypt->encode($answer, $this->key);
    }
    
    /*
     * Compares the given data to the registered equation answer.
     *
     * @access public
     * @return boolean
     */
    function decrypt_answer($answer) {
        return $this->CI->encrypt->decode($answer, $this->key);
    }
}

Also I have extended the form_validation library and tried putting this rule in there but it doesn't seem to work.


non object issue - El Forum - 01-14-2010

[eluser]jedd[/eluser]
Is there a reason you're ignoring the User Guide?

Quote:from the user guide:
File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.

You might also be having problems calling a library with the same name as the function you're currently in (but that's just wild speculation here).


non object issue - El Forum - 01-14-2010

[eluser]megabyte[/eluser]
I know you're trying to help.


but am I missing something? Is the class not a capital name?

I changed the name of the validation function, and still the same error.

Any tips on how to debug this issue?

I know you like to help and not give answers, so I am willing to put in the work.


non object issue - El Forum - 01-14-2010

[eluser]megabyte[/eluser]
Trying to debug I auto loaded the math_captcha library and no non object error.

Any ideas?


non object issue - El Forum - 01-14-2010

[eluser]Kosonome[/eluser]
Not sure, but I guess you need
Code:
$this->CI->load->library('encrypt');
inside function decrypt_answer($answer) or function Math_captcha(), and not in _init();


non object issue - El Forum - 01-14-2010

[eluser]megabyte[/eluser]
I think I solved it. It's the php4 issue they talk about in the user guide.


non object issue - El Forum - 01-14-2010

[eluser]Kosonome[/eluser]
I'm curious, please, share it. Smile


non object issue - El Forum - 01-14-2010

[eluser]megabyte[/eluser]
My next issue is trying to move my callback function into MY_Form_validation

I put it in there, change my rule and nothing happens.

Code:
$this->form_validation->set_rules('math_captcha_response', '', 'captcha[math_captcha_answer]');    


    function captcha($str, $field)
    {
        echo $str;
        
        $CI =& get_instance();
        $CI->load->library('math_captcha');
        
        $CI->form_validation->set_message('math_captcha',  
            'The "Are you human" answer is incorrect.');  
        
        if ( ! isset($_POST[$field]))
        {
            return FALSE;                
        }
        
        $field = $_POST[$field];
        
        $field = $CI->math_captcha->decrypt_answer($field);

        return ($str !== $field) ? FALSE : TRUE;
    }



non object issue - El Forum - 01-14-2010

[eluser]megabyte[/eluser]
In the user guide it states for libraries:

Quote:Also, please note: If you are running PHP 4 it's usually best to avoid calling get_instance() from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors since objects do not exist until the class is fully instantiated.


which also means it will affect controllers. So loading a library from inside the function being called I guess does not work. I created an init() function which I added to my controllers constructor and it loaded the library.


non object issue - El Forum - 01-14-2010

[eluser]jedd[/eluser]
[quote author="megabyte" date="1263528248"]
Code:
function math_captcha($str, $field)
    {
        $this->load->library('math_captcha');
[/quote]


[quote author="megabyte" date="1263530326"]
but am I missing something? Is the class not a capital name?
[/quote]

No - it's not.

Quote:I changed the name of the validation function, and still the same error.

You mean away from math_captcha ?

[quote author="jedd" date="1263530010"]
You might also be having problems calling a library with the same name as the function you're currently in (but that's just wild speculation here).
[/quote]