I have two custom modules, 'moda' and 'modb'. The source code of the .module files are as follows:

moda.module

<?php


function display_this($msg)
{
	return $msg;
}

modb.module

<?php

display_this('hey');

I have a function defined in moda.module, and I'm trying to call this function from within my modb.module file. However, this is the error I'm encountering:

Fatal error: Call to undefined function display_this() in /opt/lampp/htdocs/drupal/sites/all/modules/moda/moda.module on line 17

What seems to be wrong in my approach?

Comments

Jaypan’s picture

You never call functions directly in .module files, you declare functions in module files. The functions you declare are then called by Drupal hooks and stuff.

The problem is that when your modb.module file is loaded, the moda.module file is not yet loaded, so the function doesn't exist.

Try changing it to this:

function modb_init()
{
  display_this('hey');
}

hook_init() is called on page load.