2016-03-18 46 views
0

我正在写一个笨帮手电子邮件功能,而且需要目前在每一个功能我写笨访问模型有一种方法为所有函数加载一次CI实例?像一个构造函数方法?从助手

+0

不知道你是否看到我的回答,但我已经更新它更安全 – MonkeyZeus

+0

助手或只是一个BU多个职能,而不是一个班级。你应该为此写一个图书馆,而不是帮手。 – AdrienXL

回答

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!'; 
     } 
    } 
} 
+0

根据CI的文档,Helpers不是类:“每个帮助程序文件仅仅是特定类别中的函数的集合....与CodeIgniter中的大多数其他系统不同,Helpers不是以面向对象格式编写的。” https://ellislab.com/codeigniter/user-guide/general/helpers.html – Elymentree

+0

完全同意,但您始终可以创建图书馆 – David

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