Define and use APIs of custom modules in drupal6

Source: Internet
Author: User
Tags drupal

Drupal automatically loads all modules you have enabled, including system modules, third-party modules, and self-written modules.

So how can I provide APIs for other modules or programs? The method is very simple, that is, to write your implementation method under your. Module. For example, I have a self-written module called:


Mytest. module, in which I wrote a custom method called mytest_get_name (). The function is very simple, that is, to return a string, plus the system date of the day, as shown below:


/** <Br/> * return name + today's date <br/> * @ Param $ name <br/> * @ return string <br/> * /<br/> function mytest_get_name ($ name) {</P> <p> $ result = $ name. 'Start time :'. mytest_get_today (); <br/> return $ result; <br/>}< br/>/** <br/> * returns the current day's time <br/> */<br/> function mytest_get_today () {<br/> $ arr = date ("Y-m-d h: I: s"); <br/> return $ arr; <br/>}

After saving it, make sure that your mytest module is enabled in Drupal. In this case, if you want to call this "Work sign-in" API in another module or inc file of Drupal, you can directly write this method, for example: $ name = mytest_get_name ("Bush"); then $ name is the desired result, just like using user_load () in the Drupal user system module () or user_save.


Remember that custom methods must be written in your. Module file before they can be used.

One problem is that when my method is large and many features are implemented, many things are piled up. the module File looks messy and everyone is used to writing it in other languages. in the inc file, for example, I put mytest_get_today () in, which I wrote. After saving it, I will find that, if Zend or foreclipse IDE is used. when you type the first letter in the module, this method is automatically written. module can be used everywhere. You need to introduce this inc file to use the methods defined in Inc, just like the include statement during php development, of course, you can use the include syntax in Drupal. Drupal has its own method:


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




$ Type file type name

$ Module name

$ Name: the name of the file. If it is left empty, all types of files will be introduced.


Then I changed my program:

/** <Br/> * return name + today's date <br/> * @ Param $ name <br/> * @ return string <br/> * /<br/> function mytest_get_name ($ name) {<br/> // introduce all methods in, except for private methods <br/> module_load_include ('inc', 'mytest', 'mytest _ include '); <br/> $ result = $ name. 'Start time :'. mytest_get_today (); <br/> return $ result; <br/>}


In the file, all methods except the private method are introduced to mytest. module. left an API interface in module, and Other implementations are put in. in the inc file, the program is neat and scalable.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.