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

[eluser]James Burton[/eluser]
Hi all,

I'm starting to get the hang of using CodeIgniter.
But here's the deal.

Several parts of my website will need to send email, using the email helper, but instead of typing out all the settings each time I want to create a function that I can pass the simple details (to, subject, message) to.

Should this be done using a custom helper or library? Or does it really matter?

I've tried creating a helper but I get the following error..
Using $this when not in object context

If someone could lay out the best practice of going about this it'd be a great help. Smile

Thanks in advance!
#2

[eluser]Kepler[/eluser]
Libraries are classes, so you can have instance variables as well as functions. Helpers are groups of related functions, which is why you can call the helper functions directly (no $this-> before the function name).

It sounds like you would want to create an email helper function.
#3

[eluser]James Burton[/eluser]
Thanks, is it possible to load a library from within a helper?

So that I can load the email library, within my own custom helper.

In trying to do it I get the Using $this when not in object context error.

Code:
$this->load->library('email');

From my understanding there would need to be a way to tell php to use the codeigniter instance or something like that.
I'm just unsure how to go about that, and I've spent hours trying to google for a solution, but I'm unsure on the correct wording of what I'm looking for!
So if you could point me in the right direction it'd be a great help!

Thanks.
Smile
#4

[eluser]Dam1an[/eluser]
You can access other libraries, but you need to get an instance of the CI super object and use that instead of $this
Code:
$CI =& get_instance();
$CI->load->library('email');
$CI->email->....
#5

[eluser]James Burton[/eluser]
Thanks a lot, exactly what I was looking for.
I knew it'd be something as simple as that.
Smile
#6

[eluser]Dam1an[/eluser]
You're welcome Smile
It's also somewhere in the user guide, ah, here it is (I also found another page in the UG that I've never come accross that has it here)
#7

[eluser]James Burton[/eluser]
I was actually reading that exact page earlier, yet I somehow failed to scroll down far enough to catch that!
:lol:




Theme © iAndrew 2016 - Forum software by © MyBB