下划线方法前缀 [英] Underscore method prefix

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

问题描述

我一直在检查CodeIgniter和CakePHP的代码,我注意到它们类中的某些方法都以下划线_或双下划线__为前缀.

I've been examining the code of CodeIgniter and CakePHP and I noticed that some of the methods in their classes are prefixed with an underscore _ or a double underscore __.

这是什么目的?

推荐答案

如果不是可见性缺少适当的可见性关键字:

In the case where it is not any of PHP's magic methods, it is to indicate Visibility in lack of proper Visibility keywords:

蛋糕编码约定:

由于我们不能将PHP5的私有关键字和受保护的关键字用于方法或变量,因此我们同意以下规则:

As we cannot use PHP5's private and protected keywords for methods or variables, we agree on following rules:

  • 受保护的方法或变量名称以单个下划线("_")开头.
  • 私有方法或变量名以双下划线("__")开头.

CodeIgniter约定:

只能由您的类内部访问的方法和变量(例如,公用方法用于代码抽象的实用工具和帮助函数)应带有下划线前缀.

Methods and variables that are only accessed internally by your class, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.

这篇关于下划线方法前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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