Welcome Guest, Not a member yet? Register   Sign In
How can i load a library file in a helper file?
#1

[eluser]haraldo[/eluser]
Hi there,

Newbi question.

How can i load a library file in a helper file?

I've tryed loading the library but i get the error:

PHP Fatal error: Using $this when not in object context in...

I'm aware of what the error means. I could probably hack it but how in codeigniter can i do it nicely?!

Thanks
#2

[eluser]dcunited08[/eluser]
Code:
$ci = get_instance();
$ci->load->library('your_library');
$ci->your_library->doStuff();
#3

[eluser]haraldo[/eluser]
sweet!

Thank you
#4

[eluser]MMacdonald[/eluser]
What are you trying to achieve here? Helpers are meant to be small, procedural functions for carrying out simple tasks.

The syntax below works, but I'm not convinced that calling libraries within helpers is "good practice". I'd be interested to know other peoples' opinions.

Controller

Code:
function index()
{
  example_helper($this);
}

Helper

Code:
function example_helper($controller_name)
{
  $controller_name->load->library('calendar');
  echo $controller_name->calendar->generate(2006, 6);
}
#5

[eluser]dcunited08[/eluser]
[quote author="MMacdonald" date="1226085009"]What are you trying to achieve here? Helpers are meant to be small, procedural functions for carrying out simple tasks.

The syntax below works, but I'm not convinced that calling libraries within helpers is "good practice". I'd be interested to know other peoples' opinions.[/quote]

I would tend to agree with you and almost put that in my earlier response. It is possible to do it that way but I would not make a habit of it. My question would be why are you using a helper and not a library for this? That being said, I have done it once when I could not see another, easier option. My function loaded all the views in a particular folder.
#6

[eluser]haraldo[/eluser]
My code is as below:

Code:
function resize_image( $file_path ) {
        
    $ci = get_instance();
    
    $ci->load->library( 'image_lib' );
    $image['image_library'] = 'gd2';
    $image['source_image']    = $file_path;
    $image['create_thumb'] = TRUE;
    $image['maintain_ratio'] = TRUE;
    $image['width'] = 80;
    $image['height'] = 60;

    $ci->image_lib->initialize( $image );

    if( $ci->image_lib->resize() ) {

        $ci->image_lib->clear();
        return true;
    }

    return false;
}
#7

[eluser]dcunited08[/eluser]
I would just call the library directly and add a config file for it to hold the default values, that seems why you are putting it in a function. Another option, subclass it as MY_image_lib and create a static method that does what this does.
Code:
class MY_Image_lib extends CI_Image_lib{

var $image_library = 'gd2';
var $create_thumb = TRUE;
var $maintain_ratio = TRUE;
var $width = 80;
var $height = 60;

static function resize_image( $file_path ) {
    
    $image['source_image']    = $file_path;
    $image_lib = new MY_Image_lib($image);

    if( $image_lib->resize() ) {

        $image_lib->clear();
        return true;
    }

    return false;
}
}

That way you can call it:
Code:
$return = MY_Image_lib::resize_image($file);
#8

[eluser]haraldo[/eluser]
Thanks for the input. I may just do somrthing like that.

If you have time, would you explain your theory as to why you would do it this way, over the way i'm doing it.

Many thanks,
#9

[eluser]dcunited08[/eluser]
[quote author="haraldo" date="1226087676"]Thanks for the input. I may just do somrthing like that.

If you have time, would you explain your theory as to why you would do it this way, over the way i'm doing it.

Many thanks,[/quote]
When I am using one of CI's libraries and I want to add functionality, I extend it so that the code is all in the same place and it gets loaded automatically when I load the library. I also like to keep most items in static functions instead of stand-alone functions because it does the same thing Namespaces do in other languages, lowers the possibility of name collisions. It also allows you to know that it is associated with. I use an editor with code-assist and it helps that the function only shows up when I am attempting to use that library.




Theme © iAndrew 2016 - Forum software by © MyBB