CodeIgniter-私有功能 [英] CodeIgniter - Private functions

查看:70
本文介绍了CodeIgniter-私有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在开始使用CodeIgniter。
并且我使用他们的用户指南和其他可供学习的第三方教程。我对私有函数的命名有些困惑。根据用户指南,我必须在<$ c $前面加上前缀c> _ 以私有函数的名义。但是在本教程中,请选中添加注销部分。其中有一个私有函数:私有函数check_isvalidated()。在这里,它不以字符 _ 作为前缀。

I have started playing with CodeIgniter now. And I use their user guide and other third-party tutorials available for learning. I'm a bit stuck at the naming of private functions. Based on the user guide, I have to prefix a _ in the name of private functions. But in this tutorial, check the Add Logout section. In it, there is a private function: private function check_isvalidated(). Here, it is not prefixed with the character _.

所以这也是公认的命名约定吗?
同时,还有一个名为 _output()的对象:处理输出
这是一个公共功能,具有私有功能的命名约定!

So that's also an accepted naming convention ? At the same time, there is another one called _output(): Processing Output. It's a public function with the naming convention of a private function !

当我尝试系统地学习时,这有点令人困惑。

It's a bit of confusing when I try to learn in a systematic manner.

推荐答案

_ 前缀是<$ c $中定义的函数的约定。 c>控制器。

用户指南说:


在某些情况下,您可能希望某些功能对公共访问隐藏。要将函数设为私有,只需添加下划线作为名称前缀即可,并且不会通过URL请求提供。

In some cases you may want certain functions hidden from public access. To make a function private, simply add an underscore as the name prefix and it will not be served via a URL request.

http://www.codeigniter.com/user_guide/general/controllers.html#private-methods

添加 _ 是CodeIgniter自己在控制器(仅在控制器中)中声明不能由用户直接调用的函数的方式:

Adding an _ is CodeIgniter's own way of declaring functions in the controller (only in the controller) that cannot be called directly by the user:


  • Controller 函数映射到URL的一部分( controller / function

  • 控制器中有一些函数不应映射到URL

  • Controller functions are mapped to parts of the URL (controller/function)
  • there are functions in the controller which should NOT be mapped to the URL

- they are declared as `private` (available since PHP5)
OR
- their names start with `_` (works also for PHP4)


关于 _output 函数,它是公共的,但是由于它包含 _ ,因此无法直接调用。

Regarding _output function, it is public, but it cannot be called directly since it contains _.

为什么公开?

该函数由系统调用,因此需要从类外部进行访问,它不是私有函数。但是,它包含 _ 以确保不会通过URL调用它。

The function is called by the system, so it needs to be accessible from outside the class, it is not a private function. But, it contains _ to make sure it is not called via the URL.

总结一下,如果您控制器中有不想直接通过url调用的函数,请添加 _ 前缀或使用 private 访问运算符。

To sum up, if you have functions in your controller which you don't want to be called directly via the url, add _ prefix OR use the private access operator. Either one of them is good enough.

FYI,Yii或Zend框架等其他框架都使用 action 前缀可以通过URL调用的所有控制器功能(已映射)。

FYI, other frameworks like Yii or Zend framework, use the action prefix for all controller functions which CAN be called via the URL (are mapped).

这篇关于CodeIgniter-私有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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