PHP

Free PHP book resources & Tutorials

CHAPTER 15 - Removing a Resource

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 [...]

CHAPTER 15 - Accessing a Resource

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);

CHAPTER 15 - Writing Your First Resource-Enabled PHP Function

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;
}

CHAPTER 15 - Creating and Registering New Resources

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.

CHAPTER 15 - Resources

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 [...]

CHAPTER 15 - Summary of Example

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 [...]

CHAPTER 15 - Returning Values from PHP Functions

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 [...]

CHAPTER 15 - Memory Management

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):

CHAPTER 15 - An Introduction to Writing PHP Extensions - QUICKSTART

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:
[...]

CHAPTER 15 - An Introduction to Writing PHP Extensions

“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.
[...]

CHAPTER 14 - OPTIMIZING CODE - Rewrite in C

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 [...]

CHAPTER 14 - OPTIMIZING CODE

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 [...]

CHAPTER 14 - Content Compression

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 [...]

CHAPTER 14 - Dynamic Content Caching

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 [...]

CHAPTER 14 - Compiled Code Caching

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 [...]

CHAPTER 14 - USING ZPS (ZEND PERFORMANCE SUITE)

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.