Welcome Guest, Not a member yet? Register   Sign In
Is it possible to extend your own libraries/helpers...etc.
#1

[eluser]CMCDragonkai[/eluser]
Title says it all.

And also: How?

I'm thinking of having a base library, and then have multiple branches which extend the base library. This way when I need a specific library, use the ends of the branches, but if I only need the base library, then I'll just use it.
#2

[eluser]WanWizard[/eluser]
Yes, why not?

The only thing you have to solve is loading them, as CI doesn't support autoloading. So either implement autoloading yourself, or make sure your base library is loaded before you load the extended libraries.

This can be as easy as:
Code:
$this->load->library('base'); unset($this->base); // we don't need this one
$this->load->library('baseextended');
#3

[eluser]CMCDragonkai[/eluser]
That's a very simple solution. Thanks.

I suppose the extending would be the same as extending CI's libraries.

Code:
class Baseextended extends Base {

}

Furthermore is it possible to extend a branch which extends the base? How deep can the extensions go?
If so, how would one load them?

Also, I'm reading there are problems involving loading of models in models and loading of libraries in models.

How would one load a model in a model?

I read that loading a library in the constructor of a model requires the use of
$this->_assign_libraries();
immediately after.
However this only works if the code is placed after parent::

Is it also possible to extend models? Does the autoload work for them?

Why isn't this in the userguide?
#4

[eluser]WanWizard[/eluser]
Yes.

And you can extend as deep as you like, just make sure the parent is loaded before you load the child.

In the end, there all classes, so you can load whatever you want, whereever you want. The only issue is that not always the loaded class is available via $this, that's why the manual suggests you use get_instance().

If that bothers you, you can always do this
Code:
$this->load->library('base');
$CI =& get_instance();
$this->base =& $CI->base;
in your constructor.
#5

[eluser]CMCDragonkai[/eluser]
Ok,

I tried accessing a function that is in a custom library from one of my libraries that extends a core library.
It didn't work too well.

For example:

I have a library called "custom_library". I have extended the uri library and now have a "MY_URI" library.
In my "MY_URI" library, I have:

Code:
public function __constructor(){
    
        parent::CI_URI();
        
        if (!isset($this->CI)){
            $this->CI =& get_instance();
        }

    }

In my "custom_library" I have a function:

Code:
function hello_world(){
echo 'hello_world';
}

I tried calling hello_world() from "MY_URI" library, but there was an error about non-object.

From my "MY_URI" library I tried:
Code:
public function hello(){
$this->CI->custom_library->hello_world();
}
and
Code:
public function hello(){
$this->custom_library->hello_world();
}

Note that the core "URI" library is automatically loaded. I have also auto loaded "custom_library".

So then I go into my controller and do this:
Code:
$this->uri->hello();

And it doesn't work.
What am I doing wrong?
#6

[eluser]WanWizard[/eluser]
It might help to use __construct(), instead of __constructor()...
#7

[eluser]CMCDragonkai[/eluser]
Its interesting how CI doesn't pick up on that.

I fixed that then there was an error:

Fatal error: Call to undefined function get_instance()

When I removed this from "MY_URI" library:

Code:
if (!isset($this->CI)){
            $this->CI =& get_instance();
        }

The error was gone.

Maybe when a library is extended, there is no need to do $this->CI =& get_instance().
Maybe it's only needed when new libraries are created, or when libraries are replaced.




Theme © iAndrew 2016 - Forum software by © MyBB