Welcome Guest, Not a member yet? Register   Sign In
helper or Library
#1

[eluser]jaswinder_rana[/eluser]
I want to have the functionality of creating directories.

Now, I am not sure if it should be library or helper.

What I am trying to understand is,

- When to use helper?
- When to use library?

In CodeIgniter's context, I am trying to understand the main difference between these two.

Above is just an example. As I am new to CI, I am trying to understand the mindset when this framework was written so that I can proceed as needed (expected?).

Thanks
#2

[eluser]crumpet[/eluser]
a helper is just a collection of global functions
a library is a class

Libraries work in an object oriented way. Helpers generally just save you code
I will do an example comparing a form library and a form helper

Library Functions
set_fields(array $fields) //set all the fields in the form
set_action($action) //set the forms action
display() //prints out hte form based on the other functions you used

Helper
form_open($action) //prints a <form> tag with the action you want
form_input($name) //prints an input tag

hopefully you can see the difference
#3

[eluser]jaswinder_rana[/eluser]
Ah thanks.

So, in my case, I think, I need to write a helper for Directory making. Is that correct?

I was thinking like

mkdir_helper.php


Having said that, PHP already has that function. Will it make sense to create a helper function to just wrap that function? In my case (as of yet), I have no plans to add additional checking in there. Just creating a directory.

Thanks
#4

[eluser]crumpet[/eluser]
if it all it does is wrap the native php function then there is no point...
Just use the native. If you want to pass data to the native function in a different way then you could make a wrapper (ie it will only make one directory at a time and you to pass an array of directories to be made)
#5

[eluser]jaswinder_rana[/eluser]
Thanks

Now I have a generic questions about logic.

Let's say that I may (or may not) do that in future (i.e. may create multiple libraries or something else). Should I just build a wrapper function because I MAY need this in future??

This is often where I get stuck. I try to think too much about what I may or may not need. I just want to get advice from different perspective.

Thanks.
#6

[eluser]crumpet[/eluser]
http://c2.com/cgi/wiki?YouArentGonnaNeedIt
#7

[eluser]jaswinder_rana[/eluser]
Well that answers it. Thanks. Much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB