2

So basically, the below is the code I have right now:

class MY_Log extends CI_Log {

    /**
    * Variable storing the CodeIgniter instance.
    *
    * @access private
    * @since v0.1.0.0
    */
    private $CI;

    /**
    * Constructor for later use by internal
    * methods.
    *
    * @access public
    * @since v0.1.0.0
    */
    public function __construct()
    {
        // Extend the parent logging.
        parent::__construct();

        $this->$CI =& get_instance();
    }
}

And I get the following error;

Fatal error: Class 'CI_Controller' not found in /<deleted>/system/core/CodeIgniter.php on line 233

This is kinda how the user guide describes it.

Roel
  • 1,462
  • 4
  • 19
  • 31
  • Is there a system/core/Controller.php file within your filesystem? – dakdad Jan 16 '12 at 10:47
  • Yes, and line 233 is: return CI_Controller::get_instance(); – Roel Jan 16 '12 at 10:49
  • possible duplicate of [CodeIgniter: Class 'CI_Controller' not found](http://stackoverflow.com/questions/6758681/codeigniter-class-ci-controller-not-found) – cweiske Jan 16 '12 at 10:49
  • I haven't attempted this myself, but are you sure you need to do get_instance? In my one attempt at extending CI_Controller I seem to have access to the CI object through $this (i.e. $this->load->vars($data); works off the shelf) – danneth Jan 16 '12 at 10:57
  • I am getting "Undefined property: MY_Log::$load". Stupid libraries are driving me crazy sometimes. – Roel Jan 16 '12 at 11:02
  • @Roel where did you save you MY_Log class? – Damien Pirsy Jan 16 '12 at 11:05
  • @danneth you're talking about extending CI_COntroller, OP's extending a core library. DIfferent things. – Damien Pirsy Jan 16 '12 at 11:06
  • I am saving this in the /application/libraries folder. Saved as MY_Log.php. – Roel Jan 16 '12 at 11:11
  • @DamienPirsy That is correct, and I think/thought I made it clear it was just a guess of something to look at – danneth Jan 16 '12 at 12:05
  • Please check if your PHPMyAdmin is turned On. – Einlanzer May 14 '13 at 08:56

4 Answers4

4

I guess this error occurs when a log library function is called before CI_Controller class is loaded. This could happen in an early stage of your application routine. So you should extend the log library without using $this->$CI =& get_instance();.

Philipp Michael
  • 954
  • 1
  • 11
  • 22
3

Quite strange. I just replicated your case (with the info provided) and I encountered no problems. But make sure of a couple things:

  1. Your file is named MY_Log.php, and is located in application/libraries/My_log.php
  2. The file extends the parent class, in this case CI_Log
  3. You call the library in your controller as

    $this->load->library('log');
    $this->log->do_something();
    

    i.e, not using "My_log" but the parent library's name. In fact, you're extending it, not creating a different one, so CI wants you to call it the same as the original

  4. Your original file has the following line correctly written (without the $ sign before CI)

    $this->CI =& get_instance();
    

My test case with your code provided works fine on my development machine (Windows 7 WAMP php 5.3.8). I'll be waiting for more infos.

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • 1. That's correct. 2. Correct. 3. Indeed, doing that. 4. Had that wrong, but didn't fix the problem. It goes wrong as soon as I turn the log_treshold to 1. – Roel Jan 16 '12 at 11:43
  • Uhm...What CI version are you running? is it a fresh install? did you change something in config? Can you reproduce the issue with a brand new copy of CI? – Damien Pirsy Jan 16 '12 at 11:49
  • I changed many things in the config, but only the default values and options. base_url, index_page, url_suffix, encryption_key, sess_cookie_name and turned on global_xss_filtering. That's about it. Let me try with a fresh install. – Roel Jan 16 '12 at 11:53
  • Same result with a fresh install. CodeIgniter 2.1.0 – Roel Jan 16 '12 at 12:03
  • I'm running out of ideas...I cannot reproduce the issue neither on an existing install nor a new one; can't even tell what more infos you could provide..Do you receive the same warning? Do you get it when loading a regular library? – Damien Pirsy Jan 16 '12 at 12:29
  • No warnings. I am only doing this to store logs to the database, but this is holding me up too long so writing an own library would be faster after all. Thanks for your help. I will accept it, as it helped me the most. – Roel Jan 16 '12 at 12:51
  • Yeah, or you could maybe just add your own methods to the logging library, I was about to suggest that but I was too stubborn on this problem...Anyway, if you find the issues, please come back, write and accept your answer (or whichever will come in the future that solves the problem). Cheers! – Damien Pirsy Jan 16 '12 at 12:56
  • @DamienPirsy You should correct the file name in the given path from 'application/libraries/My_log.php' to 'application/libraries/MY_Log.php'. The correct uppercase characters are necessary if you run the application on a linux machine. Otherwise the custom library won't take effect. – Philipp Michael Nov 09 '12 at 13:53
  • The file should bi in application/libraries/code not in library folder. – rostamiani Feb 07 '19 at 04:32
0

Try putting this at the top of MY_Log.php if you haven't already:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

It's possible that your class MY_Log isn't actually being parsed by php if it's not included in the php delimiters. If that's the case, then CI would see the file MY_Log.php, and expect CI_Log to be extended by the class declared in MY_Log.php. But, if your class isn't within PHP delimiters, CI_Log wouldn't actually be extended, which could cause odd errors.

mitcheljh
  • 170
  • 1
  • 2
  • 6
0

In case this helps anyone, I had the same problem and noticed that it occurred only on URLs that contained a + in them, e.g. as part of a permalink. CI went haywire; I prevented + signs from appearing in URLs, and all was well.

Mitya
  • 33,629
  • 9
  • 60
  • 107