Welcome Guest, Not a member yet? Register   Sign In
Extend library with own class
#1

[eluser]Hyra[/eluser]
Hey hey,

can't find what I'm looking for in current threads, possibly because I'm not quite sure what to look for.

What i want to do: Extend a custom library with a custom class

On my site I will have several "Rewards" which will have a lot of the same functionality regarding loading and displaying.

So what i want to be able to write is a library like:

Code:
class CustomReward extends Reward {
// all custom functions
};

But the Reward class, sitting in the libraries folder, won't be loaded. I get a blank page if I try this.

Is it possible at all to do this?

(One alternative i haven't tried but might work is to "include" the file containing the mother-reward-class, and work with it like that, but this seems ugly.)
#2

[eluser]imn.codeartist[/eluser]
There are two ways you can create custom library class

1) create a class and place at system\libraries

if you are placing your class here then make sure your Class has prefix of CI_

for example:

Code:
class CI_Payment
{

}


2) create a class and place at system\application\libraries

if you are placing your class here then make sure your filename name has following prefix MY_

for example:

MY_library.php

Code:
class AnyClassName
{

}

now you can use your custom library and extend them
#3

[eluser]Hyra[/eluser]
Thanks for your reply. I'm familiar with libraries and where to put them.

The problem however is extending them with other custom libraries.

Let me try again to display the problem.

In:
/system/application/libraries
I want to have a file:
MY_Reward.php

Code:
class MY_Reward {
    function test() {
        echo "This is the 'mother class'";
    }
};

In the same folder I want another file:
CustomReward.php

Code:
class CustomReward extends MY_Reward {
     function __construct() {
         $this->test();
     }
     // other custom functions
};

In one of the controllers:

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

The system fails upon extending the CustomReward library with the MY_Reward class. No error message, just a blank page.

I'm wondering why, and if there's any way to get around doing this.
#4

[eluser]wiredesignz[/eluser]
There is no need to add libraries to the system/libraries directory ever.

Your library should go into application/libraries and if you want it to replace a CodeIgniter core library then name it the same as the original library, but you must use the prefix "CI_" for the class name.

If you merely want to extend a CI library then prefix it's class name and filename with the subclass_prefix "MY_" instead.

EDIT:
After reading your last post it is obvious that you need to include or require your library base class.

Do not use "MY_" in the filename unless you are extending a core library
#5

[eluser]Hyra[/eluser]
Hehe,

again .. I'm not trying to extend a CI Core library, but extend a custom library with a custom library.

But I think i'll go with the "include" approach and then extend it, as that works.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB