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.

The functions available to you are:<br />

OnUpdateBool<br />

OnUpdateInt<br />

OnUpdateReal<br />

OnUpdateString<br />

OnUpdateStringUnempty<br />

However, there may be cases where you want to check the contents of an INI setting<br />

for validity before letting it be set, or there may be things you need to call to initialize<br />

or reconfigure when one of these settings is changed. In those cases, you will have<br />

to write your own change-handling function.<br />

When you have a custom change handler, you use a simpler INI definition. In place<br />

of STD_<strong>PHP</strong>_INI_ENTRY( ), as shown previously, use:<br />

<strong>PHP</strong>_INI_ENTRY("foo.my_ini_setting", "0", <strong>PHP</strong>_INI_ALL, MyUpdateSetting)<br />

The MyUpdateSetting( ) function can then be defined like this:<br />

static <strong>PHP</strong>_INI_MH(MyUpdateSetting) {<br />

int val = atoi(new_value);<br />

if(val>10) {<br />

return FAILURE;<br />

}<br />

FOO_G(value) = val;<br />

return SUCCESS;<br />

}<br />

As you can see, the new setting is accessed via the char *new_value. Even for an integer,<br />

as in our example, you always get a char *. The full <strong>PHP</strong>_INI_MH( ) prototype<br />

macro looks like this:<br />

#define <strong>PHP</strong>_INI_MH(name) int name(zend_ini_entry *entry, char *new_value, \<br />

uint new_value_length, void *mh_arg1, \<br />

void *mh_arg2, void *mh_arg3, int stage \<br />

TSRMLS_DC)<br />

The extra mh_arg1, mh_arg2, and mh_arg3 are custom user-defined arguments that you<br />

can optionally provide in the INI_ENTRY section. Instead of using <strong>PHP</strong>_INI_ENTRY( ) to<br />

define an INI entry, use <strong>PHP</strong>_INI_ENTRY1( ) to provide one extra argument, <strong>PHP</strong>_INI_<br />

ENTRY2( ) for two, and <strong>PHP</strong>_INI_ENTRY3( ) for three.<br />

Next, after either using the built-in change handlers or creating your own, find the<br />

<strong>PHP</strong>_MINIT_FUNCTION( ) and add this after the ZEND_INIT_MODULE_GLOBALS( ) call:<br />

REGISTER_INI_ENTRIES( );<br />

In the <strong>PHP</strong>_MSHUTDOWN_FUNCTION( ), add:<br />

UNREGISTER_INI_ENTRIES( );<br />

In the <strong>PHP</strong>_MINFO_FUNCTION( ), you can add:<br />

DISPLAY_INI_ENTRIES( );<br />

This will show all the INI entries and their current settings on the phpinfo( ) page.<br />

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