Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeIgniter从helper访问模型_Codeigniter - Fatal编程技术网

CodeIgniter从helper访问模型

CodeIgniter从helper访问模型,codeigniter,Codeigniter,我正在为电子邮件函数编写CodeIgniter帮助程序,目前在我编写的每个函数中都需要: $CI = &get_instance(); $CI ->email //etc 这不是一个大问题,但我想知道是否有一种方法可以一次性加载所有函数的CI实例?类似构造函数方法的东西?您实际上可以这样做 这里有一个例子 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Ex

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

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

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

您实际上可以这样做

这里有一个例子

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

如果您完全习惯于在帮助程序中定义函数,而不是在帮助程序中定义函数,那么您可以执行以下操作:

email\u helper.php

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

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

unset($CI);

根据CI的文档,助手不是类:“每个助手文件只是特定类别中函数的集合……与CodeIgniter中的大多数其他系统不同,助手不是以面向对象的格式编写的。”完全同意,但是你总是可以创建一个库,不确定你是否看到了我的答案,但我已经将它更新为安全的LPER或只是一堆函数,而不是类。您应该为此编写一个库,而不是一个助手。