I am grouping some shared functionality that will be used across multiple template files within a custom theme. I would like certain methods within this module to be accessible to the theme (ie. public) and certain modules only available to the module itself (ie. private).

I understand that modules in Drupal aren't classes, but I hate the idea of the modules 'private' functions being available across the site - it seems to unnecessarily pollute the global namespace.

Is there a way to hide/make private certain methods/functions within a module?

2 Answers 2


I understand that modules in Drupal aren't classes

Yes, however Drupal modules can certainly make use of classes, and many do.

You can use as many classes as you like.

Put your private functions in class definitions and access them that way.

Include the class file in your module.info file:

files[] = includes/MyModuleClass.class.php

You can use public and private variables in your class

public $public_var;
private $private_var;

Add private methods to your class

// Public
public function myPublicFunction(){
  $this->public_var = 'Public';

// Private
private function myPrivateFunction(){
  $this->private_var = 'Private';        

Instantiate an object with

$options = array(); // example constructor params
$object = new MyModuleClass($options);

echo $object->public_var; // OK
echo $object->private_var; // Error

$object->myPublicFunction(); // OK
$object->myPrivateFunction(); // Error

More info in the PHP OO docs

  • 1
    This is the perfect answer - makes so much sense now you've cleared it up
    – Chris
    Apr 10, 2013 at 23:01

In drupal-7 you can use classes and a common pattern is to implement everything that is not a drupal hook in a class.

In mymodule.info add:

files[] = includes/MyModuleHelper.inc

The drupal autoloader will then make this class available throughout the site. It isn't namespaced to the specific module, but the coding standard/convention of prefixing class names with the name of the module helps (not a specific standard). Note that only classes should be loaded this way.

The convention for preventing namespace clashes on module hook functions is to prefix them with the module name. e.g. mymodule_hook_form_alter(..) or with an underscore to indicate that the function should be considered private. e.g. _mymodule_private_function(). The drupal hook system entrenches mymodule_hook_form_alter() naming by looking for specific function names for hooks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.