php 在 codeigniter 中放置全局函数的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10831213/
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
Where to place global functions in codeigniter
提问by aWebDeveloper
I have created a few utility functions like one h()that acts as echo htmlentities($var). I want theses functions to be available everywhere . so where do i put it.
我创建了一些实用程序函数,例如h()充当echo htmlentities($var). 我希望这些功能随处可用。所以我把它放在哪里。
回答by Damien Pirsy
As @david barker said, you might use an helper. Create a file named, for example, "site_helper", that contains all the functions.
正如@david barker 所说,您可以使用帮手。创建一个名为“site_helper”的文件,其中包含所有功能。
be aware you need to check if the function already exists, though, or you'll get an "function already declared" error.
但是请注意,您需要检查该函数是否已经存在,否则您将收到“函数已声明”错误。
So, something like:
所以,像这样:
file site_helper.php(in application/helpers/)
文件site_helper.php(在application/helpers/)
if(!function_exists('h'))
{
function h($value)
{
return htmlentities($value);
}
}
if(!functin_exists('other_function')
//....etc.
And then you can autoload it in config/autoload.php:
然后你可以在 config/autoload.php 中自动加载它:
$autoload['helpers'] = array('site');
^-- note how you only use the part before the underscore to call the file. Also, helpers are not classes, but a collection of functions.
^-- 请注意如何仅使用下划线之前的部分来调用文件。此外,助手不是类,而是函数的集合。
回答by Shiven
You should include your global variables file in your /application/config/constants.php file. Then move your global function file to /application/helpers folder. Then you should autoload the global functions file. /application/config/autoload.php
您应该在 /application/config/constants.php 文件中包含全局变量文件。然后将您的全局函数文件移动到 /application/helpers 文件夹。然后你应该自动加载全局函数文件。/application/config/autoload.php
$autoload['helpers'] = array('your-global-function-file.php');
I would suggest not moving anything inside of the system folder project, as upgrading would be an absolute nightmare. Sometimes refactoring your code to conform to CI logic, maybe faster in the long run rather than trying to copy/paste stuff all over the place.
我建议不要在系统文件夹项目中移动任何东西,因为升级绝对是一场噩梦。有时重构您的代码以符合 CI 逻辑,从长远来看可能会更快,而不是尝试到处复制/粘贴内容。
回答by Laurence
This is exactly what Helpers are for.
这正是 Helpers 的用途。
Create a new helper (remember to append it with _helper.php) and put it in your helper folder.
创建一个新的 helper(记得用 _helper.php 附加它)并将它放在你的 helper 文件夹中。
You can either auto load it in your config (so you can use it anywhere), or just manually load it when needed.
你可以在你的配置中自动加载它(所以你可以在任何地方使用它),或者在需要时手动加载它。
回答by digitaldonkey
Use codeigniter-kintand a custom file for global Functions.
This will give you a more helpful and beautiful Output like:

对全局函数使用codeigniter-kint和自定义文件。这将为您提供更有用和更漂亮的输出,例如:

File: application/helpers/globalfunctions_helper.php
文件:application/helpers/globalfunctions_helper.php
<?php
/**
* @file Global Helper Functions
*
*/
if(!function_exists('pr'))
{
function pr($value=false){
$CI = get_instance();
$CI->load->library('kint');
return @Kint::dump($value);
}
}
if(!function_exists('dp')){
function dp($value=false)
{
$CI = get_instance();
$CI->load->library('kint');
Kint::dump($value);
die();
}
}
See also: http://raveren.github.io/kint/

