MrD
MrD

Reputation: 5086

CodeIgniter accessing models from helper

I'm writing a CodeIgniter helper for e-mail functions, and need currently in each function I'm writing:

$CI = &get_instance();
$CI
->email
//etc

It's not a big problem, but I was wondering if there was a way of loading the CI instance once for all functions? Something like a constructor method?

Upvotes: 1

Views: 843

Answers (2)

David
David

Reputation: 1145

You can actually do this.

Here's an example

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Example {
    public function __construct()
    {
        $this->CI =& get_instance();
    }
    public function functionExample(){

        $this->CI->load->module('fullpage');
        if($this->CI->fullpage->my_function() ){
           echo 'It works!'; 
        }
    }
}

Upvotes: 0

MonkeyZeus
MonkeyZeus

Reputation: 20737

If you are absolutely set on defining your functions inside of a helper rather than extending CodeIgniter's email library then you can do this:

email_helper.php

<?php
// Assuming $CI has not been set before this point
$CI = &get_instance();

function some_email_function()
{
    $GLOBALS['CI']->email;
}

unset($CI);

Upvotes: 2

Related Questions