I am reading the Drupal api docs. I'm looking under functions. At the end there are a lot of functions that start with an underscore. Some of them (all?) seem to be something called "helper functions." I've looked in my php books, searched drupal.org and on Google for "helper functions" mixed with "underscore" and "php." Came up with no answers. What's special about the underscore and functions that start with it? What does the underscore mean?

Comments

Heine’s picture

Function names starting with an underscore are considered private and shouldn't be called by external functions. Be aware that it's a convention, there's nothing to enforce this.
--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.

ashraf.hussain’s picture

Thanks Heine, your answer is precise and to the point.

webchick’s picture

While Drupal is not written in an object-oriented way, and PHP prior to version 5 didn't support the concept of private vs. public functions, an underscore preceding a function is a way to indicate that the intention is for this function to be "private" (can also be read as "internal") which means it should only be called by the module that declared it. In Drupal's case, it's simply a coding standard. You can call _forum_get_vid from the blahblah.module just fine.

webchick’s picture

Oops, too slow. ;)

mlanning’s picture

Is it acceptable to define the function in an included file (.inc) and call it in the module file (.module)?.. or should the call be defined within the same file only?

Jaypan’s picture

It's fine to do this, however note that you'll need to include the .inc file using module_load_include() before the function can be used.

dman’s picture

I'm surprised you couldn't google it, but this convention is common through a lot of programming languages, not just Drupal, and not just PHP.

Also, the implication for a programmer is that you should NOT call those functions from outside the module (that's what private means) as the interface (like, the order of arguments or the data structure being passed in) may change without warning. The collary is that non-private (public) functions are there for you to call whenever you want.
You are welcome to call other modules functions from your own whenever you want.
... in theory.

.dan.
http://www.coders.co.nz/

mwu’s picture

thank you everyone