Module_load_include () vs require_once

I was wondering when you need to use module_load_include() or require_once to include files located in your module.

+6
source share
2 answers

The main thing that the Drupal module_load_include() function does over standard PHP require_once is that it refers to the module path when searching for a file using drupal_get_path() .

If you used require_once , you would need to make this bit yourself.

Another thing is to check whether the file exists before it is included, which is convenient for preventing fatal crashes, but it makes no sense if you still get it when you try to call the functions that you tried to include. This is convenient, although it allows you to create more meaningful errors.

At the end of the day, module_load_include() is actually just a small useful feature provided by Drupal to make things a little easier for yourself. If you know where the file is and you know that it exists there, very little need to use the Drupal function; you can just use require_once .

+13
source

module_load_include requires that Drupal be fully loaded (fully loaded).

: module_load_include ($ type, $ module, $ name = NULL);

For example: module_load_include('inc','module_name','file_name');

If you want to use this function in a global context, use require_once

require_once does not need.

For example: require_once DRUPAL_ROOT . '/path/file' . require_once DRUPAL_ROOT . '/path/file' .

+1
source

Source: https://habr.com/ru/post/896407/


All Articles