05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Fleshing Out the Skeleton<br />

The rot13.c file contains the C code that implements the extension. After including a<br />

standard collection of header files, the first important part of the extension is:<br />

/* {{{ rot13_functions[]<br />

*<br />

* every user-visible function must have an entry in rot13_functions[]<br />

*/<br />

function_entry rot13_functions[] = {<br />

<strong>PHP</strong>_FE(confirm_rot13_compiled, NULL) /* for testing; remove later */<br />

<strong>PHP</strong>_FE(rot13, NULL)<br />

{NULL, NULL, NULL} /* must be the last line in rot13_functions[] */<br />

};<br />

/* }}} */<br />

The {{{ and }}} sequences in the comments don’t have meaning to the C compiler or<br />

<strong>PHP</strong>—they indicate a “fold” to editors that understand text folding. If your editor<br />

supports it (Vim6 and Emacs do), you can represent a block of text (e.g., a function<br />

definition) with a single line (e.g., a description of the function). This makes it easier<br />

to edit large files.<br />

The important part in this code is the function_entry array, which lists the uservisible<br />

functions that this extension implements. Two such functions are shown<br />

here. The ext_skel tool generated the confirm_rot13_compiled( ) function for the purposes<br />

of testing. The rot13( ) function came from the definition in rot13.def.<br />

<strong>PHP</strong>_FE( ) is a macro that stands for <strong>PHP</strong> Function Entry. The <strong>PHP</strong> API has many<br />

such convenience macros. While they speed up development for programmers experienced<br />

with the API, they add to the learning curve for beginners.<br />

Next comes the zend_module_entry struct:<br />

zend_module_entry rot13_module_entry = {<br />

STANDARD_MODULE_HEADER,<br />

"rot13",<br />

rot13_functions,<br />

<strong>PHP</strong>_MINIT(rot13),<br />

<strong>PHP</strong>_MSHUTDOWN(rot13),<br />

<strong>PHP</strong>_RINIT(rot13), /* replace with NULL if no request init code */<br />

<strong>PHP</strong>_RSHUTDOWN(rot13), /* replace with NULL if no request shutdown code */<br />

<strong>PHP</strong>_MINFO(rot13),<br />

"0.1", /* replace with version number for your extension */<br />

STANDARD_MODULE_PROPERTIES<br />

};<br />

This defines the functions to be called for the various stages of startup and shutdown.<br />

Like most extensions, rot13 doesn’t need per-request startup and shutdown<br />

functions, so follow the instructions in the comments and replace <strong>PHP</strong>_RINIT(rot13)<br />

and <strong>PHP</strong>_RSHUTDOWN(rot13) with NULL. The resulting zend_module_entry struct looks<br />

like this:<br />

322 | Chapter 14: Extending <strong>PHP</strong><br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!