Reputation: 8970
I am new to Code igniter / OOP but trying to figure this out.
I am trying to make a helper that I can use in my code; this is what it looks like:
if ( ! function_exists('email'))
{
function email($type, $to, $subject, $object)
{
switch($type){
case 'new':
$body = "Hello ". $object['FirstName'] . ' ' . $object['LastName'] . ","
. "<p/><p/>Thank you.";
break;
}
// Send it
$this->load->library('email');
$this->email->to($to);
$this->email->from('[email protected]', 'James');
$this->email->subject($subject);
$this->email->message($body);
$this->email->send();
}
}
I then include it in the autoload for the helper section.
When i try to access it within my controller, I get an error.
$obect['FirstName']='Carl';
$obect['LastName']='Blah';
email('new', '[email protected]', 'test', $object);
Here is the error that I am getting:
Fatal error: Using $this when not in object context in C:\inetpub\wwwroot\attrition\application\helpers\email_helper.php on line 17
Upvotes: 0
Views: 525
Reputation: 511
you'll use that variable instead of $this
so, your $this is change by this
$CI =& get_instance();
How to use? usualy you use $this like
$this->load->other();
// change to
$CI->load->other();
It should be work
Upvotes: 1
Reputation: 4592
Using $this when not in object context
This simply means you can not use the $this keyword outside of an object(class), as @Kryten pointed out.
Helpers are generally only used for embedding in html, such as formatting data.
<p><?php echo formatHelper(escape($var)); ?></p>
What you need to do is read up a little on creating a Library.
Upvotes: 0
Reputation: 569
change your function to this code :
if ( ! function_exists('email'))
{
function email($type, $to, $subject, $object)
{
switch($type){
case 'new':
$body = "Hello ". $object['FirstName'] . ' ' . $object['LastName'] . ","
. "<p/><p/>Thank you.";
break;
}
// Send it
$this = &get_instance(); // here you need to get instance of codeigniter for use it
$this->load->library('email');
$this->email->to($to);
$this->email->from('[email protected]', 'James');
$this->email->subject($subject);
$this->email->message($body);
$this->email->send();
}
}
Upvotes: 0