开发者

What's the meaning of _function() despite of function() [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Underscore method prefix

I am browsing the Code Igniter library and system and I keep finding functions that have an underscore _function() and functions that has not function(); this happens even inside classes. Now a question rise up:

What is the meaning of the unde开发者_如何学Pythonrscore before a function?


It's a common coding convention to denote private methods. The same pertains to CodeIgniter, see the docs as quoted by Pekka and Gordon.

In PHP 5 (5.1 being the minimum reqiurement from CI 2.0 onward), you can use the private access modifier instead, which actually enforces private access (i.e. causes errors when you try to call the method from outside the class).


I don't know about code igniter but an underscore usually identifies functions/variables which are used internally and shouldn't be called from outside that object.


Usually something with a leading underscore is private and something with two leading underscores is "special" (eg. the __toString() magic method).


From CodeIgniter's coding conventions:

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.

convert_text()        // public method
 _convert_text()      // private method


CodeIgniter regards the _function() as a private function as opposed to just function().

the main use of this is in the CodeIgniter controllers since you can access all the controller's functions via url directly.

except for those with preceding _

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜