tags:

views:

101

answers:

3

if i've got string functions i use a lot, should i put them in a helper class or a library class?

functions like: truncate string if longer than 30 characters, return a random string, make all lower cases and so on. these are functions that i probably don't need to create an object for. it's better to use them as static methods.

should i put them in a library class or a helper class?

when do i know when to put where?

+2  A: 

Helpers are the classes that help something already there for example there can be a helper for:

array
string
url
etc

A library is something that can be any solution; it could be created for the first time by you and no one else has created.

Because you are dealing with a string (something already there), you should put it in a helper class, or modify the string helper class of the framework (if there is one). However, this is a convention or standard but you can create a library for it too if you are creating something really cool for string handling with quite some functions.

Sarfraz
could you say that library classes should be used to instantiate objects. if so, then it's easy to know when to create a helper and when to create a library class. but in java, we got Math class. and that got only static functions. back to square one:)
weng
@noname: could not understand your comment, but i did not talk about initiating or not initiating.
Sarfraz
+1  A: 

I assume you are using CodeIgniter.

Since you already write that you don't need to instantiate an object and will use it in it's static methods, then making it into helper will be make sense than making it into library.

In CI, helpers is also managed, once loaded, second attempt to load it will be omitted. You can open the CI's build in helper to learn what it does, then compare it with libraries. By knowing the purpose, you can then decide yourself, helpers or libraries.

Donny Kurnia
A: 

if i've got string functions i use a lot, should i put them in a helper class or a library class?

If they are functions, why do you want to stick them in a class? PHP allows for free floating functions.

troelskn
PHP allows a lot, why it sometimes can get messy. so i wanted to know what is the best structure.
weng
In case it wasn't clear, I was implying that I think free functions are the best structure for the case described.
troelskn