php 如何从 CodeIgniter 中的模型加载助手?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1680721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:29:28  来源:igfitidea点击:

How to load helper from model in CodeIgniter?

phpclasscodeigniter

提问by Simon Perepelitsa

I want to load some helper in a model. How to do this? Tried to use:

我想在模型中加载一些帮助程序。这该怎么做?尝试使用:

${get_parent_class($this)}->load->helper ('text');

But still getting an error

但是还是报错

Fatal error: Call to a member function helper() on a non-object

致命错误:在非对象上调用成员函数 helper()

采纳答案by GSto

$this->load->helper('helpername')

回答by mwm

GSto answered $this->load->helper('helpername')but if you are in a model's method, $thissimply refers to that model's (class) instance and not to CI global. That won't work!

GSto 回答了,$this->load->helper('helpername')但如果您在模型的方法中,$this只需引用该模型的(类)实例而不是 CI 全局。那行不通!

Instead you need to load the CI global and then load the helper:

相反,您需要加载 CI 全局,然后加载助手:

// PHP 4
// $ci =& get_instance();
// PHP 5    
$ci = get_instance();
$ci->load->helper('text');

回答by oparam

You are not need to load helper in a model.Just load helper in a controller and use function in a model as well as we normally use helper function in a controller

您不需要在模型中加载助手。只需在控制器中加载助手并在模型中使用函数,就像我们通常在控制器中使用助手函数一样

回答by Tudor

I think CI doesnt check for helper duplication ... CI herlpers are procedural files , you might include ur helper twice if ur controller has the same helper loaded as ur model (that is loaded in that controller). Maybe do a library instead...

我认为 CI 不会检查 helper 重复... CI herlpers 是程序文件,如果您的控制器与您的模型加载了相同的帮助程序(即在该控制器中加载),您可能会包含您的助手两次。也许做一个图书馆......

I can see i get negative votes w/o any comments ... by checking loader class from core CI u can see helpers method isnt checking if the helper has been loaded before (it isn't included in is_loaded() array like most classes that are loaded through load factory class) ... I dont recommend anyway to load helpers in both models and controllers ... for ex i made a helper for output encoding that i use in controllers (before i pass data to the view) . It would be very bad if i change the view state twice ...

我可以看到我得到了没有任何评论的反对票......通过检查核心 CI 中的加载器类,你可以看到 helpers 方法没有检查 helper 之前是否已加载(它不像大多数类那样包含在 is_loaded() 数组中)通过加载工厂类加载)......无论如何我不建议在模型和控制器中加载助手......例如,我为我在控制器中使用的输出编码制作了一个助手(在我将数据传递给视图之前)。如果我两次更改视图状态会非常糟糕......