Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

CHAPTER 22 DEVELOPMENT BEST PRACTICES

It is useful to know if a function is a menu callback (that is, mapped to a URL using hook_menu()):

/**

* Menu callback; prints a listing of all books. */

function book_render() {

...

}

Documenting Hook Implementations

When a function is a hook implementation, there is no need to document the hook. Simply state which hook is being implemented, as in the following example:

/**

* Implements hook_theme(). */

function statistics_theme(){

...

}

Including Code

Anywhere you are unconditionally including a class file, use required_once(). Anywhere you are including a class file, use include_once(). Either of these will ensure that class files are only included once. They share the same file list, so you don’t need to worry about mixing them. A file included with require_once() will not be included again by a call to include_once(). An example of using require_once is as follows:

require_once(DRUPAL_ROOT . '/' . variable_get('cache_inc', 'includes/cache.inc'));

PHP Code Tags

Always use <?php ?> to delimit PHP code and not the shorthand <? ?>. This is required for Drupal compliance and is also the most portable way to include PHP code on different operating systems. The ?> is always omitted from the end of a code file; this includes modules and include files. The reasons for this include the following:

1.Eliminating the possibility for unwanted whitespace at the end of files, which can cause “header already sent” errors, XHTML/XML validation issues, and other problems

2.The closing delimiter is optional.

3.PHP.net itself removes the closing delimiter from the end of its file, setting the best practice.

You should, however, use the closing ?> tag when you are mixing PHP and HTML and there is HTML that follows the PHP code.

495

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]