0

I'm using native session library to replace the built in session library in CI. I need to extend the class but when I drop in MY_Session.php, CI reverts back to the old /system/libraries/Session.php.

How to I extend a class that's replaced a core CI class like Session.php?

ringerce
  • 533
  • 2
  • 12
  • 25

1 Answers1

1

Simply by naming your class files identically to a native library will cause CodeIgniter to use it instead of the native one. To use this feature you must name the file and the class declaration exactly the same as the native library. For example, to replace the native Email library you'll create a file named application/libraries/Email.php

-user guide

then call it

class MY_Email extends CI_Email {

    public function __construct()
    {
        parent::__construct();
    }
}

Loading Your Sub-class:

$this->load->library('email');

EDIT Try this:

Just load your new library (the one doing the extending):

Then, let's say we have Session.php and Mysession.php

<?php
load_class('session', false);

class Mysession extends Session {
   //your code
}  

You don't need the MY_ name tag still, I think you want to reserve that for it's original intended purpose to avoid confusion.

.. else just use an include() or require() :P

MikeCruz13
  • 1,254
  • 1
  • 10
  • 19
  • Thanks for the reply. Maybe I didn't explain what I need to do well. I'm using this library http://codeigniter.com/wiki/Session_Hybrid .. I dropped the Session.php file in application/libraries/ and create a MY_Session.php file to extend the custom session class but when I do this it defaults back to CI's native session management. I was trying to figure out a way to override the custom class's methods without modifiying it's code. – ringerce May 18 '12 at 09:48
  • You don't need the MY_ in the filename. Just name it Session.php and it'll replace the old one. – MikeCruz13 May 18 '12 at 10:51
  • Ok let me explain a bit more. I've taken Session.php from the custom Session Hybrid library and dropped it in my /application/libraries folder. Now I want to extend this custom library without replacing code in the file. When I create MY_Session.php in the library folder it doesn't load /application/libraries/Session.php anymore. It defaults back to /system/libraries/Session.php. Make more sense ?? :) – ringerce May 19 '12 at 08:53
  • Ok, i get what you're after now. Edited answer. Sorry for being dense :P – MikeCruz13 May 19 '12 at 09:17
  • That works. Thanks for the answer. I was curious if their was a way to use MY_ tag but I guess that's only intended for extending the CI native classes. Thanks again. – ringerce May 19 '12 at 10:28