CodeIgniter Forums
Getting "Undefined property" error message - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Getting "Undefined property" error message (/thread-31319.html)



Getting "Undefined property" error message - El Forum - 06-14-2010

[eluser]Armando Ramos[/eluser]
Hi, I'm new to CI.

I recently created my own library file called "Error_handle.php" in "application/libraries" that handles any run time error.

I updated my autoload.php file to this:
$autoload['libraries'] = array('authorizor', 'error_handler');

Here is the code in which I call my the class from my library file:

Code:
function index()
    {
        if($this->authorizor->user_authorized())
        {
            $user = $_SESSION[session_id()];
            //print_r($user);
            try
            {
                $q = "";
                if($user[0]['Role']['name'] == 'admin')
                {
                    $q = Doctrine_Query::create()
                        ->from('Rsewrite_Map rm')
                        ->leftJoin('rm.User u');
                }
                else
                {
                    $q = Doctrine_Query::create()
                    ->from('Rewrite_Map')
                    ->where('u_id = ?', $user[0]['id'])
                    ->andWhere('disabled = ?', '0');
                }
                $rewrite_map = $q->fetchArray();
                //print_r($rewrite_map);
                
                $data['rewrite_map'] = $rewrite_map;
                $this->load->view("rm_records_view", $data);
            }
            catch(Exception $e)
            {
                $this->error_handler->get_error($e, $_SERVER['HTTP_REFERER']);
            }
        }
    }

However, I get the following error:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: RM_Records::$error_handler

Filename: controllers/rm_records.php

What am I doing wrong?


Getting "Undefined property" error message - El Forum - 06-14-2010

[eluser]daelsepara[/eluser]
perhaps in the constructor section of your controller, you can place the line.

Code:
$this->load->library('error_handler');



Getting "Undefined property" error message - El Forum - 06-15-2010

[eluser]Armando Ramos[/eluser]
Did that and it did not work.


Getting "Undefined property" error message - El Forum - 06-16-2010

[eluser]mddd[/eluser]
Inside a controller or model, $this means: the CI object.
But inside a library, $this means: the library. You have to get the CI object and then call the other library through that object.

So you should change this:
Code:
$this->error_handler->get_error($e, $_SERVER['HTTP_REFERER']);
into this:
Code:
$CI =& get_instance(); // get a reference to the CI object
$CI->error_handler->get_error($e, $_SERVER['HTTP_REFERER']); // call the error through the CI object



Getting "Undefined property" error message - El Forum - 06-16-2010

[eluser]Armando Ramos[/eluser]
It worked! Thanks for the help!


Getting "Undefined property" error message - El Forum - 06-16-2010

[eluser]danmontgomery[/eluser]
[quote author="mddd" date="1276693125"]Inside a controller or model, $this means: the CI object.[/quote]

This is incorrect... $this represents whatever object you're currently working in. In the controller, it's the controller class. In the model, it's the model class. The model just inherits most of the controller's members when it's instantiated (See Model::_assign_libraries).