0

What is the difference between declaring the files[] in a module's .info file and declaring the file inside a hook_menu() item?

According to the Drupal Documentation on .info files...

When a module is enabled, Drupal will rescan all declared files and index all the >classes and interfaces that it finds. Classes will be loaded automatically by PHP >when they are first accessed.

And the documentation on the files key for hook_menu() is...

A file that will be included before the page callback is called; this allows page >callback functions to be in separate files. The file should be relative to the >implementing module's directory unless otherwise specified by the "file path" >option.

This sounds a little redundant to me. Aren't the files being "included" when you declare them in the .info file? So why would i need to list the file location for a function inside hook_menu when it should already be included?

1
  • 1
    Do you know difference between interfaces and classes, and functions? If yes, edit your question because it does not make much sense. If no, it is PHP question, not Drupal one.
    – Mołot
    Commented Dec 6, 2014 at 19:47

2 Answers 2

1

I'm sorry you do not understand my question, however, I believe the question was pretty straightforward. After discovering some interesting articles I think worked out my confusion.

The files[] entry in the module .info file is not used for "including" php files. It is meant only to auto load your classes and interfaces for your application (if you're writing your module using object oriented methodologies). It could be thought of as the "import" statement in Java.

The following articles go more into detail about PSR-0 standard and auto loading in php. previousnext.com.au/blog/drupal-8-now-psr-0-code-drupal-7/ riceball.com/d/book/export/html/1200

That being said, the purpose of the file key inside the hook_menu() $item[] is to specify where the file containing the page_callback function exists. If your page_callback function is defined inside a class (statically), then you do not have to provide the file and filepath keys. The callback would look something like this, 'page_callback' => array('MyClass::myfunction');

For more information on OO Page callbacks in Drupal 7, read the following article. previousnext.com.au/blog/object-oriented-page-callbacks-drupal-7

If your page_callback is not inside a class, but just inside a file then you'll need to provide the file and filepath keys so that Drupal can include this file when your page is created.

1
  • I'm not sure if you understood it 100%, because from your comment I understand that if you use class in menu hook it will be autoloaded, but it is not. if you use classes as menu callback you still need to take care how classes are loaded. 3 ways to do that: 1. you give the path where class is in menu callback. 2. you put file path of the class inside .info file. 3. you use xautoload, that modules automatically loads classes which are defined in PSR-0 standard way
    – Mantas
    Commented Dec 8, 2014 at 0:24
0

in files from .info it scans for classes and interfaces. When you use class somewhere in code, file is included automatically. You don't need to include it

In hook_menu you define in which file your callback function is.

not sure how is it redundant. how would you be able to switch one with another?

1
  • Two different situations. 1) What if my callback function is inside a class that specified in the .info file? The class has already been added so why would i need to include that file again? 2) What if i don't have any classes and my files are just a bunch of defined functions? Do i not add those to the .info file? If i do add them, is specifying them in hook_menu() now redundant?
    – Nick
    Commented Dec 6, 2014 at 17:58

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