I'm creating a module just to learn, so it doesn't do anything interesting.

The folders structure is

testmodule
| -- include
|      | - file1.inc
|      | - file2.inc
|
| - testmodule.module
| - testmodule.info

Inside testmodule.module I have a switch and according to a variable I have to call a function contained into include/file1.inc or into include/file2.inc

this is the code

function testmodule_call_external_functions($type)
{
	switch ( $type )
	{
		case 'function1':
			echo testmodule_function1(); //this is into file1.inc
			break;

		case 'function2':
			echo testmodule_function2(); //this is into file2.inc
			break;
	}
}

Of course at the moment I have a "call to undefined function" in both cases.
I have already included those files into testmodule.info

How do I say to Drupal where to find those functions?

Comments

nevets’s picture

Not a very Drupal way of doing things.

In Drupal, unless your module is providing an API, functions are generally triggered through menu callbacks registered in hook_menu() which for each menu callback allows you to define which file the callback function is in. Functions that are shared by the module are most easily defined in the main module file (yourmodule.module).

Wilted’s picture

I see. I'll put those functions inside the testmodule.module file.
Thank you.

ayesh’s picture

Adding the PHP files to the .info file with files[] notation is actually more of suggesting, and they only work for interfaces and classes.

You can include the PHP file before calling the function.

function testmodule_call_external_functions($type)
{
    switch ( $type )
    {
        case 'function1':
            module_load_include('inc', 'testmodule', 'include/file1');
            echo testmodule_function1(); //this is into file1.inc
            break;
        case 'function2':
            module_load_include('inc', 'testmodule', 'include/file2');
            echo testmodule_function2(); //this is into file2.inc
            break;
    }
}
Wilted’s picture

Thank you.

nazarioa’s picture

I have some code in mymodule.module file which removes stored data (variable_del). A user can add or remove a feature at will which add or removes it from the database.

I also want to remove data that gets stored in the database by implementing hook_uninstall(). I am a big fan of reusing code where it makes sense. In this case, the logic to remove data is already written and sits in mymodule.module, is the method you mentioned a good way to reuse this code?

nevets’s picture

You should be able to just call any function in a .module file.