General developer forum

Problems with locallib.php file

Picture of Saúl Chacón Grimaldo
Problems with locallib.php file

Hello everyone.

I am in the development of a module and i try to use the file locallib.php to create functions of my module, it happens that when i reach the line where i import the file or when i call the function that i created, i get a message Error or simply the page is left blank.

I call a function after checking for a button

require(dirname(FILE).'/locallib.php'); //i have also tried require_once, include and include_once
if (isset($formdata->button)) {
    if (validation) {
        newmodule_function_name($var1, $var2, $var3);

The locallib file is very basic

defined('MOODLE_INTERNAL') || die();
require(dirname(FILE).'/file.php'); //I use a class
 * Function description
 * @param string $var1
 * @param string $var2
 * @param array $var3
function newmodule_function_name($var1, $var2, $var3) {
    // code

The function inside the file does not return anything, and so i leave it empty without any code, it still does not work. i do not know what i'm doing wrong, if someone could tell me how i could use that file i would appreciate it.

Average of ratings: -
Picture of Karen Holland
Re: Problems with locallib.php file
Moodle HQParticularly helpful MoodlersPlugin developersTesters

Hi Saúl, good luck with your plugin development!

I'd suggest including your files with the following:

$ require_once(__DIR__.'/nameoffile.php');

This is a standard Moodle way of defining the current directory's relative pathway, used in many areas (you can grep the core code for examples).

Also check out at the "Require / include" section for more details. and is also excellent for an introduction to moodle plugin development

Also I'd highly recommend Moodle 3.1 LTS modules development by Tomasz Muras, very comprehensive clear explanations and very readable.

Average of ratings: -