CodeIgniter从帮助程序访问模型

时间:2016-03-18 12:33:18

标签: codeigniter

我正在为电子邮件功能编写CodeIgniter助手,目前我正在编写的每个函数中都需要:

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

这不是一个大问题,但我想知道是否有一种方法可以为所有功能加载CI实例一次?像构造函数方法的东西?

2 个答案:

答案 0 :(得分:1)

如果你完全在帮助器而不是extending CodeIgniter's email library内定义你的功能,那么你可以这样做:

<强> 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);

答案 1 :(得分:0)

你实际上可以这样做。

这是一个例子

<?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!'; 
        }
    }
}
相关问题