• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split library into 2 versions, 'light' and 'complete', then call functions from either using just 1 library name.

#1
[eluser]tical[/eluser]
I have a library that is quite large in size and when loaded uses about 1.2mb of memory.

Most of functions in the library are rarely needed, whilst others are often used on every page.

To reduce the wasted memory usage, I want to split the library into 2 new libraries, a 'Light' version containing the common functions, and then another version that extends the light version with the remaining functions.

I can obviously do this by just simply creating 2 library files and calling the version I need on each page.
I have tried doing this and it reduces the memory usage to only 0.4mb for the Light version - exactly what I was looking to do.

However, a problem that occurred, is that it can start to get confusing as to which library contains what functions when calling them.

Provided that the correct library is initialised in the first place, for example:
Code:
$this->load->library('my_light_library');
or
Code:
$this->load->library('my_complete_library');

Is it possible to call functions from them using the same library name?
Code:
$this->my_library->my_function()

#2
[eluser]wh1tel1te[/eluser]
Code:
$this->load->library('my_light_library', '', 'my_lib');

or:

Code:
$this->load->library('my_complete_library', '', 'my_lib');

then:

Code:
$this->my_lib->some_function();

More info here.

#3
[eluser]InsiteFX[/eluser]
wh1tel1te,

You can pass an array to the library but you cannot give it a different name!
You can only do that with CI Models.

Code:
$this->load->library('my_complete_library', $array);

InsiteFX

#4
[eluser]wh1tel1te[/eluser]
[quote author="InsiteFX" date="1299182457"]wh1tel1te,

You can pass an array to the library but you cannot give it a different name!
You can only do that with CI Models.

Code:
$this->load->library('my_complete_library', $array);

InsiteFX[/quote]
You enter the custom library name as the third parameter, not the second, which is an array as you suggest. Have a closer look at my code again. In your case:

Code:
$this->load->library('my_complete_library', $array, 'my_custom_lib_name');
$this->my_custom_lib_name->some_function();

Just tested with one of my own libraries (CI 2.0), works fine.

Code:
$this->load->library('Flexauth', '', 'jujubee');
if ($this->jujubee->is_logged_in() == TRUE)
{
    echo 'Logged in';
} else
{
    echo 'Not logged in';
}

No errors. Outputted text as expected.

#5
[eluser]tical[/eluser]
Thanks wh1tel1te, it worked exactly how I wanted.

For anyone else trying to do the same kind of thing, I then encountered another problem, that I've also since solved.

The 'Complete' version of the library needed to extend the 'Light' library, rather than duplicating the same functions from the light file in the complete file.

As of at least CI2.0, to extend a custom library use the following code:
This is in the 'Complete' library file, the 'Light' file is constructed as normal.
Code:
load_class('Light_library', 'libraries', FALSE);

class Complete_library extends Light_library
{
    public function __construct()
    {
        parent::__construct();
    }

    function your_other_functions()
    {
        ...
    }
}

Note: The 'load_class()' function is a CI system file and is liable to change in future CI versions. If anyone has a safer method of extending a custom library, please let me know.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.