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.

Some elements of the SAPI globals structure are themselves structures with fields.<br />

For example, to access the request_uri, use:<br />

SG(request_info).request_uri<br />

Executor Globals (EG)<br />

These are runtime globals defined internally by the Zend executor. The most common<br />

EG variables are symbol_table (which holds the main symbol table) and active_<br />

symbol_table (which holds the currently visible symbols).<br />

For example, to see if the user-space $foo variable has been set, you could do:<br />

zval **tmp;<br />

if(zend_hash_find(&EG(symbol_table), "foo", sizeof("foo"),<br />

(void **)&tmp) == SUCCESS) {<br />

RETURN_STRINGL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));<br />

} else {<br />

RETURN_FALSE;<br />

}<br />

Internal Extension Globals<br />

Sometimes you need extensionwide global C variables. Since an extension has to be<br />

thread-safe, global variables are a problem. You can solve this problem by creating a<br />

struct—each would-be global variable becomes a field in the struct. When compiled<br />

as a thread-safe extension, macros take care of passing this struct around. When<br />

compiled as a non-thread-safe extension, the struct is a true global struct that is<br />

accessed directly. This way, the non-thread-safe builds do not suffer the slight performance<br />

penalty of passing around this global struct.<br />

These macros look something like this for a thread-safe build:<br />

#define TSRMLS_FETCH( ) void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL)<br />

#define TSRMG(id,type,el) (((type) (*((void ***) \<br />

tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->el)<br />

#define TSRMLS_D void ***tsrm_ls<br />

#define TSRMLS_DC , TSRMLS_D<br />

#define TSRMLS_C tsrm_ls<br />

#define TSRMLS_CC , TSRMLS_C<br />

For the non-thread-safe build, they don’t do anything and are simply defined as:<br />

#define TSRMLS_FETCH( )<br />

#define TSRMLS_D void<br />

#define TSRMLS_DC<br />

#define TSRMLS_C<br />

#define TSRMLS_CC<br />

#endif /* ZTS */<br />

So, to create extensionwide global variables, you first need to create a struct in which<br />

to store them, along with the thread-safe and non-thread-safe access macros.<br />

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