To remove a resource, you usually want to
use the following macro:
int zend_list_delete(int id)
The macro is passed the id of the resource, and returns either SUCCESS or
FAILURE. If the resource exists, prior to removing it from the Zend resource list,
it will call the registered destructor for the resource type. Therefore, in our
example, you don’t have to [...]
To access a resource, you need to use the fol-
lowing macro (see Table 15.6 for an explanation of its arguments):
ZEND_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id,
resource_type_name, resource_type);
Implementing
file_open() should now be easy, and it should look as follows:
PHP_FUNCTION(file_open)
{
char *filename = NULL;
char *mode = NULL;
int argc = ZEND_NUM_ARGS();
int filename_len;
int mode_len;
FILE *fp;
if (zend_parse_parameters(argc TSRMLS_CC, “ss”, &filename,
&filename_len, &mode, &mode_len) == FAILURE) {
return;
}
We are about to imple-
ment the file_open() function. After we open the file and receive a FILE *, we
need to register it with the resource mechanism. The main macro to achieve
this is
ZEND_REGISTER_RESOURCE(rsrc_result, rsrc_pointer, rsrc_type);
An Introduction to Writing PHP Extensions Chap. 15
See Table 15.4 for an explanation of the macro’s arguments.
A resource is an abstract value that can hold any kind
of information. As previously mentioned, this information often consists of
data such as file handles, database connection structures, and other complex
types.
An Introduction to Writing PHP Extensions Chap. 15
The main reason for using resources is that they are managed via a cen-
tralized list that automatically destroys the [...]
You have learned how to write a simple PHP function. Going back to the
beginning of this chapter, we mentioned two main motivations for writing
PHP functionality in C. The first was to write some of your algorithms in C for
performance or for functionality reasons. The previous example should allow
you to quickly get started with these kind [...]
The extension API includes a rich collection of macros that allows you to
return values from your functions. These macros come in two main flavors.
The first is of the form RETVAL_type(), which sets the return value but your C
code keeps on executing. This is usually used if you still want to do some clean-
ing up before [...]
PHP’s API for allocating memory from the heap is almost identical to the stan-
dard C API. When writing extensions, use the following API functions that
correspond to their C counterparts (and therefore are not explained):
Instead of slowly explaining some of the building blocks of the scripting
engine, this section dives into coding an extension, so do not worry if you don’t
see the whole picture right away.
Imagine you are writing a web site but need a function, which will repeat
a string n times. Writing this in PHP is simple:
[...]
“If the code and the comments disagree, then both are probably wrong.”–
Norm Schryer
One of the main reasons for PHP’s success is the large amount of available
extensions. No matter what a web developer might need, he’ll most probably
find it in the PHP distribution, including extensions that support various
databases, graphic file formats, compression, XML technologies, and lots
more.
[...]
Sometimes, it is just not possible to optimize a piece of PHP code. The code is
as fast as it possibly can be in PHP, but it may still be a bottleneck. This is the
time to wield your axe, chop it to bits, and rewrite it in C as a PHP extension.
If you have some C [...]
This section covers techniques for finding miscellaneous optimizations, includ-
ing micro-benchmarks, rewriting PHP code in C, and writing procedural ver-
sus object-oriented code.
Micro-Benchmarks
Often, you may find yourself wondering which approach is the fastest. For
example, which is faster–str_replace() or preg_replace()–for a simple
replacement? You can find the answer to many of these questions by writing a
little micro-benchmark that measures [...]
Compression of HTTP pages is one of the best-kept secrets of the web. Few
people know that, but literally all the major browsers today are capable of
working with compressed content, decompress it on-the-fly, and show it as if it
was uncompressed. If properly implemented, the use of content compression
can result in the reduction of around 90 percent [...]
Dynamic content caching is by far the most effective way to boost perfor-
mance, in the cases where it is applicable, because it eliminates both the com-
pilation and execution overhead of your application. In simple terms, content
caching means saving the results of your application, typically HTML con-
tent, and then sending it out as-is again when another [...]
The Zend Performance Suite’s Acceleration module, which performs com-
piled code caching, is the simplest and often the most effective way to speed up
your application. To understand what Acceleration does, we first need to go
back to the execution architecture of the Zend Engine. In the previous section,
you saw how the engine first compiles your PHP files [...]
ZPS is a commercial product from Zend.com. ZPS provides tools for
Automatic Optimization. By using the Zend Optimizer (http://
www.zend.com/store/products/zend-optimizer.php), you can improve your
performance by 20 percent without making any code changes.