CodeIgniter Forums

Full Version: Call to a member function validate() on a non-object using own library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi;

Why do I get these error?

1 -
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: CI

Filename: libraries/Generate_token.php

Line Number: 15  ( Line 15 is $CI->validate($token);)

2 - http://grab.by/E7A2

Main controller:
PHP Code:
$this->load->library('generate_token');
 
    echo $token $this->generate_token->generate(); 


Library:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//Custom error handling
class Generate_token  
{
    public function 
__construct()
        {
            
$CI =& get_instance();
        }
      
    public function 
generate()
        {
            
$raw_random_bytes mcrypt_create_iv(6MCRYPT_DEV_URANDOM);
            
$encoded_random_bytes base64_encode($raw_random_bytes);
            
$token strtr(trim($encoded_random_bytes'='), array('+' => '-''/' => '_'));
            if(
$CI->validate($token)==$token)
                {
                    return 
true;
                }
        }
    public function 
validate($token)
        {
            
$sql "SELECT id FROM url_tokens WHERE short_url = ?";
            
$query $CI->db->query($sql, array($token));
            if(
$query->num_rows==0)
                {
                    return 
$token;
                }
            else
                {
                    
$CI->generate();
                }    
        }    


Cheers
I'm assuming this error means $CI is not an object. Like its not being defined like
Code:
$CI = New Generate_token();
If you loaded this like
Code:
$this->load->library('generate_token');
Then i think you might try to
Code:
$this->generate_token->validate($token);

//Please anybody correct me if i'm wrong. I'm also very new to CI but i'm trying to help as much as i can with my little knowledge of PHP
(01-26-2015, 04:15 AM)stopz Wrote: [ -> ]I'm assuming this error means $CI is not an object. Like its not being defined like
Code:
$CI = New Generate_token();
If you loaded this like
Code:
$this->load->library('generate_token');
Then i think you might try to
Code:
$this->generate_token->validate($token);

//Please anybody correct me if i'm wrong. I'm also very new to CI but i'm trying to help as much as i can with my little knowledge of PHP

Hey friend;
Doesn't really make sense what said. I want to call generate() not validate().

I am following everything like the manual Sad
try:

Code:
$result = $this->generate_token->generate();
(01-26-2015, 04:29 AM)stopz Wrote: [ -> ]try:


Code:
$result = $this->generate_token->generate();

That is exactly what I am doing Smile See post #1
I fixed it but I don't like the solution! I am surprised it works this way!

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//Custom error handling
class Generate_token   
{
 public function 
generate()
 {
 
           $CI =& get_instance();
 
           $raw_random_bytes mcrypt_create_iv(6MCRYPT_DEV_URANDOM);
 
       $encoded_random_bytes base64_encode($raw_random_bytes);
 
       $token strtr(trim($encoded_random_bytes'='), array('+' => '-''/' => '_'));
 
           if($CI->generate_token->validate($token)==$token)
 
            {
 
            return $token;
 
            }
 
       }
 public function 
validate($token)
 {
 
$CI =& get_instance();
 
$sql "SELECT id FROM url_tokens WHERE short_url = ?";
 
       $query $CI->db->query($sql, array($token));
 
       if($query->num_rows==0)
 
           {
 
               return $token;
 
           }
 
       else
         
{
 
        $CI->generate();
 
           
 


Just a notice...

When you have in constructor : $CI =& get_instance();

You can not reuse this variable into methods, because it is only at constructur..
Instead make:

$this->CI =& get_instance();

And at methods replace $CI with $this->CI
(01-26-2015, 04:40 AM)sv3tli0 Wrote: [ -> ]Just a notice...

When you have in constructor : $CI =& get_instance();

You can not reuse this variable into methods, because it is only at constructur..
Instead make:

$this->CI =& get_instance();

And at methods replace $CI with $this->CI


Hey;

That's not what the manual says!

PHP Code:
$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc
(01-26-2015, 05:05 AM)behnampmdg3 Wrote: [ -> ]That's not what the manual says!

PHP Code:
$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc

I am talking about pure PHP nothing related to CI..

Here is working example of your first post class

PHP Code:
class Generate_token  
{
    public function 
__construct()
        {
            
$this->CI =& get_instance();
        }
      
    public function 
generate()
        {
            
$raw_random_bytes mcrypt_create_iv(6MCRYPT_DEV_URANDOM);
            
$encoded_random_bytes base64_encode($raw_random_bytes);
            
$token strtr(trim($encoded_random_bytes'='), array('+' => '-''/' => '_'));
            if(
$this->validate($token)==$token)
                {
                    return 
true;
                }
        }
    public function 
validate($token)
        {
            
$sql "SELECT id FROM url_tokens WHERE short_url = ?";
            
$query $this->CI->db->query($sql, array($token));
            if(
$query->num_rows==0)
                {
                    return 
$token;
                }
            else
                {
                    
$this->generate();
                }    
        }    


When you are in Class you can call methods from the same class using $this->methodname()
When you are in class and want to call method from some class loaded with CI you can use $this->CI->classs->method name() but first inside class constructor you have to set $this->CI to get instance of CI..
Sure, you can load the CI superglobal object in EACH of your classes methods if you wish (like the very limited example in the userguide shows), or you can make it available to all methods.

Code:
class Yourclass {
  private $CI; //create a attribute for the CI superglobal

  public function __construct()
  {
    $this->CI =& get_instance(); //make CI available to all methods
  }

  public function some_method()
  {
    //use the CI property instead of using $CI =& get_instance() in each method
    $this->CI->load->helper('url');
  }

  public function another_method()
  {
    //again, we already loaded CI in the __construct(), so just use it!
    $some_var = $this->CI->input->post('some_var', TRUE);
  }
}