php - 是否可以在 codeigniter 的助手中使用多个 $this

标签 php codeigniter

我正在创建一个表单错误记录器,但由于我的 register.php Controller 已经满了,我想将它移到一个帮助程序,但问题是我不能使用 $this。

是的,我已经检查过,有一些解决这个问题的答案是:

function test()
{
    $CI =& get_instance();
    $CI->load->database();
    echo $CI->db->hostname; // give the config name here (hostname).
}

(引自Acess database config variables from a helper)

但是我的问题是我只能为每个函数使用一个模型并且看到我试图移动的代码是这样的:

function submitCheck() {
    $this->load->model("User");
    if(empty($post)) { //If anything is empty the view will be loaded
        $this->load->view('includes/header.php');
        $this->load->view('error_message.php'); 
        $this->load->view('includes/footer.php'); 

        if(empty($post['firstname'])) //This is for the error log
        {  
            $this->load->helper('array', 'error'); //Loading the helper
            echo $msg = errorLog('firstname');
            $this->load->view('error_message.php');
        }        

        if(empty($post['mail'])) //This is for the error log
        {
            $this->load->helper('error'); //Loading the helper
            echo $msg = errorLog('mail');
        }
    }
    else
    {
        echo "Everything is filled in";
    }
}

因此,如果遵循代码示例,我必须为每个 $this 制作大约 4/5 个函数。我应该创建一个加载器来加载其他加载器,还是可以使用 user_loader 加载其他 View /模型。

我是 codeIgniter 的初学者,所以我可能只是觉得太难了,有一个简单的修复方法,但我找不到。提供任何帮助

提前致谢!

最佳答案

根据 CodeIgniter documentation :

Unlike most other systems in CodeIgniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.

不建议在您的情况下使用助手。最好定义您的自定义库来管理您的大部分代码。

关于php - 是否可以在 codeigniter 的助手中使用多个 $this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30591977/

相关文章:

php - 从沙盒切换到 paypal live

php - 函数内部的数据库查询未发布到页面

javascript - 如何将 jquery 添加到 wordpress 插件

php - jquery 幻灯片和淡入淡出使用 cookie 在重新加载时保持打开状态

php - codeigniter:按数据类别获取多个表中的百分比

php - 如果表中不存在ID,则在表中插入多条记录

php - 在 CodeIgniter 1.7 中获取原始 SQL 查询

php - ModX Revo : Query Multiple TVs?

php - 尝试使用 ajax 将参数传递给模式?

php - 如何检查更新是否完成