在codeigniter中放置全局函数的地方 [英] Where to place global functions in codeigniter
问题描述
我已经创建了一些效果函数,像一个 h()
,作为 echo htmlentities($ var)
。我想要这些功能在任何地方都可用。
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.
推荐答案
正如@david barker所说,你可能使用一个助手。创建一个名为例如site_helper的文件,其中包含所有函数。
As @david barker said, you might use an helper. Create a file named, for example, "site_helper", that contains all the functions.
请注意,您需要检查函数是否已经存在,会得到一个已声明的函数错误。
be aware you need to check if the function already exists, though, or you'll get an "function already declared" error.
所以,像:
文件 site_helper.php
(在 application / helpers /
中)
if(!function_exists('h'))
{
function h($value)
{
return htmlentities($value);
}
}
if(!functin_exists('other_function')
//....etc.
然后你可以在config / autoload.php中自动加载它:
And then you can autoload it in config/autoload.php:
$autoload['helpers'] = array('site');
- 注意如何只使用下划线之前的部分来调用文件。此外,helpers不是类,而是函数的集合。
^-- note how you only use the part before the underscore to call the file. Also, helpers are not classes, but a collection of functions.
这篇关于在codeigniter中放置全局函数的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!