05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

php_info_print_table_start( );<br />

php_info_print_table_header(2, "rot13 support", "enabled");<br />

php_info_print_table_end( );<br />

When you remove a function (such as RINIT( ) or RSHUTDOWN( )) from rot13.c, be sure<br />

to remove the corresponding prototype from php_rot13.h.<br />

The MINFO( ) function is called by phpinfo( ) and adds whatever information you<br />

want about your extension to the phpinfo( ) output.<br />

Finally, we get to the functions that are callable from <strong>PHP</strong>. The confirm_rot13_<br />

compiled( ) function exists only to confirm the successful compilation and loading of<br />

the rot13 extension. The skeleton tests use this. Most experienced extension writers<br />

remove the compilation-check function.<br />

Here is the stub function that ext_skel created for our rot13( ) function:<br />

/* {{{ proto string rot13(string arg)<br />

returns the rot13 version of arg */<br />

<strong>PHP</strong>_FUNCTION(rot13)<br />

{<br />

char *arg = NULL;<br />

int argc = ZEND_NUM_ARGS( );<br />

int arg_len;<br />

if (zend_parse_parameters(argc TSRMLS_CC, "s", &arg, &arg_len)<br />

== FAILURE)<br />

return;<br />

php_error(E_WARNING, "rot13: not yet implemented");<br />

}<br />

/* }}} */<br />

The {{{ proto line is not only used for folding in the editor, but is also parsed by the<br />

genfunclist and genfuncsummary scripts that are part of the <strong>PHP</strong> documentation<br />

project. If you are never going to distribute your extension and have no ambitions to<br />

have it bundled with <strong>PHP</strong>, you can remove these comments.<br />

The <strong>PHP</strong>_FUNCTION( ) macro declares the function. The actual symbol for the function<br />

is zif_rot13, which is useful to know if you are debugging your code and wish to set<br />

a breakpoint.<br />

The only thing the stubbed function does is accept a single string argument and then<br />

issue a warning saying it hasn’t been implemented yet. Here is a complete rot13( )<br />

function:<br />

<strong>PHP</strong>_FUNCTION(rot13) {<br />

char *arg = NULL, *ch, cap;<br />

int arg_len, i, argc = ZEND_NUM_ARGS( );<br />

if (zend_parse_parameters(argc TSRMLS_CC, "s/", &arg, &arg_len)<br />

== FAILURE)<br />

return;<br />

324 | 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!