gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:30:21 25 4
gpt4 key购买 nike

我正在创建一个表单错误记录器,但由于我的 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/

25 4 0
文章推荐: php - 使用签名算法版本 4 创建签名的 S3 下载链接
文章推荐: php - 如何通过 PHP 对静态图像应用 pagecurl 效果
文章推荐: php - 如何设置