Welcome Guest, Not a member yet? Register   Sign In
helper, plugin, library -> function vs. class
#1

[eluser]searain[/eluser]
So helper is a collection of functions.
Plugin is usually a single function.
Library is for class.

So we can create our own library, our own plugin, and our own helper.

My question is what factors will be involved when we decide that should we make it a helper/plugin (function) or should we make it a library (class)?

In some pure OOP languages, everything is a object of a class. But in php and CI, sometimes we make functions, sometimes we make classes, for example, in the CI helper, we have helper as download, in the CI library, we have a class of upload.

Download is a helper a collection of functions in CI, upload is a class in CI library, what factors contribute to CI's decision about making download a helper and making upload a class?
#2

[eluser]Dam1an[/eluser]
When you use a class, you normally have certain data associated with that 'object', which is shared among many differant functions.So in the upload class, which you specifically mentioned, there are the followig class variables
Code:
var $max_size        = 0;
var $max_width        = 0;
var $max_height        = 0;
var $max_filename    = 0;
var $allowed_types    = "";
var $file_temp        = "";
var $file_name        = "";
var $orig_name        = "";
var $file_type        = "";
var $file_size        = "";
var $file_ext        = "";
var $upload_path    = "";
var $overwrite        = FALSE;
var $encrypt_name    = FALSE;
var $is_image        = FALSE;
var $image_width    = '';
var $image_height    = '';
var $image_type        = '';
var $image_size_str    = '';
var $error_msg        = array();
var $mimes            = array();
var $remove_spaces    = TRUE;
var $xss_clean        = FALSE;
var $temp_prefix    = "temp_file_";

Sure, you could pass the required variables into a helper style function, but things start to get messy.
Also (not sure if its the case in this particular class) the class variables might get manipulated by other functions.

Also, a lot of these variables will get set in a constructor, when you create the object, so if you have a user object, you might set stuff like the user_id, name, surname etc based on the user_id in the session. This way you don't need to manually fetch these from the database each time.

I hope that kind of explains things a bit
#3

[eluser]wiredesignz[/eluser]
An alternative way of thinking might be that because PHP4 did not allow the proper use of static classes all helpers had to be functions to allow for use in global context. (ie: in views, controllers, models etc)

Now when using PHP5 only it probably is ok to use a helper class provided that it remains static. (can't be instantiated) And it remains in global context.

I also see plugins as classes that you wish to load but instantiate manually at some later point in any context.

And libraries are classes loaded and used in context of the controller ($CI) only.




Theme © iAndrew 2016 - Forum software by © MyBB