在 codeigniter 中放置全局函数的位置 [英] Where to place global functions in codeigniter

查看:38
本文介绍了在 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.

所以,例如:

file site_helper.php(在 application/helpers/ 中)

file site_helper.php (in 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');

^-- 请注意如何仅使用下划线之前的部分来调用文件.另外,helper 不是类,而是函数的集合.

^-- 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆