CodeIgniter Forums
Extending Native Libraries or Replacing Native Libraries with Your Versions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Extending Native Libraries or Replacing Native Libraries with Your Versions (/showthread.php?tid=66138)



Extending Native Libraries or Replacing Native Libraries with Your Versions - xirah - 09-12-2016

Hi all,
I want to extend the Log library to customize the log format. I have create my own class MY_Log extending CI_Log in application/libraries. When I load this library I have this error: Unable to load the requested class: Log.
I have copied the original Log class system/core/Log to application/libraries to replace the native one, but I have the same error.
I am using codeigniter 3.0.6
Can somebody help me to solve this issue ?


RE: Extending Native Libraries or Replacing Native Libraries with Your Versions - InsiteFX - 09-12-2016

In the future fyi:

If the Library you are extending is in the:

system/core then it goes in application/core

system/libraries it goes in application/libraries


RE: Extending Native Libraries or Replacing Native Libraries with Your Versions - dave friend - 09-12-2016

You do not need to copy the original file when extending a Core class. In fact, you should not do that.

To extend the log library it would look something like this

Code:
class My_Log extends CI_Log {

  protected $my_custom_var;

   public function __construct()
   {
     parent :: __construct();
     //your stuff here
     $this->my_custom_var = "Custom Loggers Are Great!";
   }

   //your new class function
    public function my_special_logger()
   {
       //your code
   }

    //overwrite a base class function
    public function write_log($level, $msg)
    {
       //your file writing routines here (which might include copying code from Log.php in this case)
    }

}



RE: Extending Native Libraries or Replacing Native Libraries with Your Versions - xirah - 09-13-2016

(09-12-2016, 09:31 AM)InsiteFX Wrote: In the future fyi:

If the Library you are extending is in the:

system/core then it goes in application/core

system/libraries it goes in application/libraries

I have noticed it. Thanks.