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 struct looks like this in the php_foo.h header file:<br />

ZEND_BEGIN_MODULE_GLOBALS(foo)<br />

int some_integer;<br />

char *some_string;<br />

ZEND_END_MODULE_GLOBALS(foo)<br />

#ifdef ZTS<br />

# define FOO_G(v) TSRMG(foo_globals_id, zend_foo_globals *, v)<br />

#else<br />

# define FOO_G(v) (foo_globals.v)<br />

#endif<br />

The ext_skel tool creates most of this for you. You simply have to uncomment the<br />

right sections.<br />

In the main extension file, foo.c, you need to declare that your extension has globals<br />

and define a function to initialize each member of your global struct:<br />

ZEND_DECLARE_MODULE_GLOBALS(foo)<br />

static void php_foo_init_globals(zend_foo_globals *foo_globals)<br />

{<br />

foo_globals->some_integer = 0;<br />

foo_globals->some_string = NULL;<br />

}<br />

To have your initialization function called on module initialization, add this inside<br />

the <strong>PHP</strong>_MINIT_FUNCTION( ):<br />

ZEND_INIT_MODULE_GLOBALS(foo, php_foo_init_globals, NULL);<br />

To access one of these globals, some_integer or some_string, use FOO_G(some_<br />

integer) or FOO_G(some_string). Note that the struct must be available in the function<br />

in order to use the FOO_G( ) macro. For all standard <strong>PHP</strong> functions, the global<br />

struct is automatically and invisibly passed in.<br />

However, if you write your own utility functions that need to access the global values,<br />

you’ll have to pass in the struct yourself. The TSRMLS_CC macro does this for you,<br />

so calls to your utility functions look like:<br />

foo_utility_function(my_arg TSRMLS_CC);<br />

When you declare foo_utility_function( ), use the TSRMLS_DC macro to receive the<br />

global struct:<br />

static void foo_utility_function(int my_arg TSRMLS_DC);<br />

Creating Variables<br />

As we saw in the previous section, the symbol_table and active_symbol_table hashes<br />

contain user-accessible variables. You can inject new variables or change existing<br />

ones in these hashes.<br />

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

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Creating Variables | 345

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

Saved successfully!

Ooh no, something went wrong!