text
stringlengths 1
254
| output
stringlengths 2
255
|
---|---|
This same form of "extended copying" is also necessary for PHP's zval containers.
|
Zend ships with such a function, called zend_copy_ctor() (the previous PHP equivalent was pval_copy_constructor()).
|
For strings, proper memory allocation would have to be assured, and so on.
|
A most useful demonstration is a function that accepts a complex type as argument, modifies it, and then returns the argument:
|
The copy constructor' s counterpart in the Zend API, the destructor zval_dtor(), does the opposite of the constructor.
|
The copy constructor's counterpart in the Zend API, the destructor zval_dtor(), does the opposite of the constructor.
|
Example 28-1 shows the source.
|
We'll start with the creation of a very simple extension at first, which basically does nothing more than implement a function that returns the integer it receives as parameter. 例28-1 shows the source.
|
A simple extension.
|
例 28-1A simple extension.
|
We' ll explain the source code in detail shortly, but first we'd like to discuss the build process. (This will allow the impatient to experiment before we dive into API discussions.)
|
This code contains a complete PHP module. We'll explain the source code in detail shortly, but first we'd like to discuss the build process. (This will allow the impatient to experiment before we dive into API discussions.)
|
The example source makes use of some features introduced with the Zend version used in PHP 4.1.0 and above, it won 't compile with older PHP 4.0.x versions.
|
注意 The example source makes use of some features introduced with the Zend version used in PHP 4.1.0 and above, it won't compile with older PHP 4.0.x versions.
|
Use the provided "make" mechanism in the ext directory, which also allows building of dynamic loadable modules.
|
There are basically two ways to compile modules:
|
The second method is good for those who (for some reason) don't have the full PHP source tree available, don't have access to all files, or just like to juggle with their keyboard.
|
Use the provided "make" mechanism in the ext directory, which also allows building of dynamic loadable modules.
|
Compiling Using Make.
|
Compile the sources manually.
|
Enables userland module --enable-cross_conversion BOOK:
|
After you run buildconf, configure --help shows the following additional modules:
|
Enables first module --enable-infoprint BOOK:
|
The module shown earlier in 例28-1 can be enabled with --enable-first_module or --enable-first_module=yes.
|
Enables resource test module --enable-variable_creation BOOK:
|
Compiling Manually. To compile your modules manually, you need the following commands:
|
Required items are the PHP directory, the Zend directory, and (if necessary), the directory in which your module resides.
|
The link command is also a plain vanilla command instructing linkage as a dynamic module.
|
You can include optimization options in the compilation command, although these have been omitted in this example (but some are included in the makefile template described in an earlier section).
|
You can include optimization options in the compilation command, although these have been omitted in this example (but some are included in the makefile template described in an earlier section).
|
Prev
|
Note: Compiling and linking manually as a static module into the PHP binary involves very long instructions and thus is not discussed here. (It's not very efficient to type all those commands.)
|
Those who know don 't talk.
|
Those who know don't talk.
|
Sometimes, PHP "as is" simply isn't enough.
|
Those who talk don't know.
|
Prev
|
As soon as this point is reached, it's time to touch the heart of PHP and take a look at its core, the C code that makes PHP go.
|
To create an .ini section in your own module, use the macros PHP_INI_BEGIN() to mark the beginning of such a section and PHP_INI_END() to mark its end.
|
PHP 4 features a redesigned initialization file support. It's now possible to specify default initialization entries directly in your code, read and change these values at runtime, and create message handlers for change notifications.
|
PHP_INI_BEGIN() PHP_INI_ENTRY("first_ini_entry", "has_string_value", PHP_INI_ALL, NULL) PHP_INI_ENTRY("second_ini_entry", "2", PHP_INI_SYSTEM, OnChangeSecond) PHP_INI_ENTRY("third_ini_entry", "xyz", PHP_INI_USER, NULL) PHP_INI_END()
|
To create an .ini section in your own module, use the macros PHP_INI_BEGIN() to mark the beginning of such a section and PHP_INI_END() to mark its end. In between you can use PHP_INI_ENTRY() to create entries.
|
#define PHP_INI_MH( name) int name(php_ini_entry *entry, char *new_value, uint new_value_length, void *mh_arg1, void *mh_arg2, void *mh_arg3)
|
The fourth parameter consists of a pointer to a change-notification handler. Whenever one of these initialization entries is changed, this handler is called. Such a handler can be declared using the PHP_INI_MH macro:
|
INI_ORIG_INT( name)
|
Access to initialization entries can also be handled with the macros shown in 表39-1.
|
Finally, you have to introduce your initialization entries to PHP.
|
表 39-1Macros to Access Initialization Entries in PHP
|
ZEND_MINIT_FUNCTION( mymodule) {REGISTER_INI_ENTRIES();} ZEND_MSHUTDOWN_FUNCTION(mymodule) {UNREGISTER_INI_ENTRIES();}
|
Finally, you have to introduce your initialization entries to PHP. This can be done in the module startup and shutdown functions, using the macros REGISTER_INI_ENTRIES() and UNREGISTER_INI_ENTRIES():
|
Zend features a single API for these types - they 're stored using hash tables.
|
Complex types such as arrays and objects require different treatment. Zend features a single API for these types - they're stored using hash tables.
|
A discussion about creating more advanced types follows later in this chapter.
|
注意 To reduce complexity in the following source examples, we're only working with simple types such as integers at first. A discussion about creating more advanced types follows later in this chapter.
|
They behave exactly like their C counterparts, but provide virtual working directory support on the thread level.
|
The following directory and file functions should be used in Zend modules. They behave exactly like their C counterparts, but provide virtual working directory support on the thread level.
|
We won 't go into this issue here, however, as you should already have this most basic ability when studying this chapter.
|
Make sure that you can compile a working PHP environment by yourself! We won't go into this issue here, however, as you should already have this most basic ability when studying this chapter.
|
This is a must-have ability to implement and debug extensions.
|
Before we start discussing code issues, you should familiarize yourself with the source tree to be able to quickly navigate through PHP's files. This is a must-have ability to implement and debug extensions.
|
Everything else is below this directory.
|
The following table describes the contents of the major directories.
|
From PHP 4.0, it' s possible to compile these standard extensions as dynamic loadable modules (at least, those that support it).
|
Discussing all the files included in the PHP package is beyond the scope of this chapter. However, you should take a close look at the following files:
|
Discussing all the files included in the PHP package is beyond the scope of this chapter.
|
php4/main/php.h, located in the main PHP directory. This file contains most of PHP's macro and API definitions.
|
php4 / main / php.h, located in the main PHP directory.
|
php4/Zend/zend.h, located in the main Zend directory. This file contains most of Zend's macros and definitions.
|
php4 / Zend / zend.h, located in the main Zend directory.
|
php4/Zend/zend_API.h, also located in the Zend directory, which defines Zend's API.
|
Zend is built using certain conventions; to avoid breaking its standards, you should follow the rules described in the following sections.
|
Zend is built using certain conventions; to avoid breaking its standards, you should follow the rules described in the following sections.
|
This should only be done with extreme care, however, and only in conjunction with demands of the Zend API; otherwise, you risk memory leaks.
|
To allocate resident memory that survives termination of the current script, you can use malloc() and free(). This should only be done with extreme care, however, and only in conjunction with demands of the Zend API; otherwise, you risk memory leaks.
|
The term PHP refers to the complete system as it appears from the outside.
|
The interpreter part analyzes the input code, translates it, and executes it.
|
To implement a Web script interpreter, you need three parts:
|
The functionality part implements the functionality of the language (its functions, etc.).
|
The functionality part implements the functionality of the language (its functions, etc.).
|
The interface part talks to the Web server, etc.
|
Figure 24-1.
|
図 24-1The internal structure of PHP.
|
The following sections discuss where PHP can be extended and how it 's done.
|
The following sections discuss where PHP can be extended and how it's done.
|
The following sections discuss these options.
|
As shown in 図24-1 above, PHP can be extended primarily at three points: external modules, built-in modules, and the Zend engine. The following sections discuss these options.
|
Finally, the line number currently being executed can be retrieved using the function zend_get_executed_lineno().
|
Finally, the line number currently being executed can be retrieved using the function zend_get_executed_lineno(). This function also requires the executor globals as arguments. For examples of these functions, see 例36-2.
|
For examples of these functions, see Example 36-2.
|
例 36-2Printing execution information.
|
zend_printf() works like the standard printf(), except that it prints to Zend's output stream.
|
zend_printf() works like the standard printf(), except that it prints to Zend's output stream.
|
PHP automatically prints a section in phpinfo() for you if you specify the ZEND_MINFO function, including the module name in the heading.
|
PHP automatically prints a section in phpinfo() for you if you specify the ZEND_MINFO function, including the module name in the heading. Everything else must be formatted and printed by you.
|
To print the table footer, use php_info_print_table_end().
|
例 36-1 Source code and screenshot for output in phpinfo().
|
zend_error( E_WARNING, "This function has been called with empty arguments");
|
zend_error() can be used to generate error messages. This function accepts two arguments; the first is the error type (see zend_errors.h), and the second is the error message.
|
Note that by default the display of this type of error messages is turned off in php.ini.
|
表 36-1Zend's Predefined Error Messages.
|
Internal warning by the compiler; shouldn't be used by user-written modules.
|
図 36-1Display of warning messages in the browser.
|
Table 35-1.
|
注意 The macros in 表35-1 automatically return from your function, those in 表35-2 only set the return value; they don't return from your function.
|
RETVAL_STRINGL( string, length, duplicate)
|
表 35-1Predefined Macros for Returning Values from a Function
|
This macro is faster and binary-safe, and should be used whenever the string length is known.
|
表 35-2Predefined Macros for Setting the Return Value of a Function
|
The module startup and shutdown functions are called whenever a module is loaded and needs initialization; the request startup and shutdown functions are called every time a request is processed (meaning that a file is being executed).
|
The module startup and shutdown functions are called whenever a module is loaded and needs initialization; the request startup and shutdown functions are called every time a request is processed (meaning that a file is being executed).
|
For dynamic extensions, module and request startup / shutdown events happen at the same time.
|
For dynamic extensions, module and request startup/shutdown events happen at the same time.
|
Prev
|
Declaration and implementation of these functions can be done with macros; see the earlier section "Declaration of the Zend Module Block" for details.
|
Since the interpreter and executor core have been separated from the main PHP package, a second API defining macros and function sets has evolved: the Zend API.
|
To declare functions that are to be exported (i.e., made available to PHP as new native functions), Zend provides a set of macros. A sample declaration looks like this:
|
The parameter list of this declaration is very important; you should keep these parameters in mind (see Table 31-1 for descriptions).
|
The parameter list of this declaration is very important; you should keep these parameters in mind (see 表31-1 for descriptions).
|
0 indicates that the return value is not used; 1 indicates that the caller expects a return value.
|
表 31-1Zend's Parameters to Functions Called from PHP
|
typedef struct _zend_function_entry {char *fname; void (*handler)(INTERNAL_FUNCTION_PARAMETERS); unsigned char *func_arg_types;} zend_function_entry;
|
例 31-1Internal declaration of zend_function_entry.
|
The macro ZEND_FE (short for'Zend Function Entry') simply expands to a structure entry in zend_function_entry.
|
注意 You cannot use the predefined macros for the end marker, as these would try to refer to a function named "NULL"!
|
For example, ZEND_FE( "first_module", NULL) introduces a function first_module() to PHP and links it to the C function zif_first_module().
|
Tip: Compilation errors that refer to functions named zif_*() relate to functions defined with ZEND_FE.
|
Use this function if you don 't want the automatic name prefixing introduced by ZEND_FE.
|
表31-2 shows a list of all the macros that you can use to define functions.
|
Doesn' t require a corresponding C function; refers to the alias target instead.
|
表 31-2Macros for Defining Functions
|
The function implementation is surrounded by a conditional compilation statement.
|
This function is special to all dynamic loadable modules. Take a look at the creation via the ZEND_GET_MODULE macro first:
|
get_module() is called by Zend at load time of the module.
|
get_module() is called by Zend at load time of the module. You can think of it as being invoked by the dl() call in your script. Its purpose is to pass the module information block back to Zend in order to inform the engine about the module contents.
|
If you don' t implement a get_module() function in your dynamic loadable module, Zend will compliment you with an error message when trying to access it.
|
If you don't implement a get_module() function in your dynamic loadable module, Zend will compliment you with an error message when trying to access it.
|
This file makes all macros and API definitions required to build new modules available to your code.
|
The only header file you really have to include for your modules is php.h, located in the PHP directory. This file makes all macros and API definitions required to build new modules available to your code.
|
Now that you' ve got a safe build environment and you're able to include the modules into PHP files, it's time to discuss how everything works.
|
Now that you've got a safe build environment and you're able to include the modules into PHP files, it's time to discuss how everything works.
|
All PHP modules follow a common structure:
|
All PHP modules follow a common structure:
|
C declaration of exported functions (required to declare the Zend function block)
|
Header file inclusions (to include all required macros, API definitions, etc.)
|
Declaration of the Zend module block
|
C declaration of exported functions (required to declare the Zend function block)
|
Troubleshooting
|
Implementation of get_module()
|
The example function in first_module looks like this:
|
Implementing the exported functions is the final step. The example function in first_module looks like this:
|
After the declaration, code for checking and retrieving the function' s arguments, argument conversion, and return value generation follows (more on this later).
|
After the declaration, code for checking and retrieving the function's arguments, argument conversion, and return value generation follows (more on this later).
|
To mark this field as unused, use NULL.
|
This block is stored in the structure zend_module_entry and contains all necessary information to describe the contents of this module to Zend. You can see the internal definition of this module in 例31-2.
|
Such a version string can look like this (in chronological order): "2.5-dev ", "2.5RC1", "2.5 "or "2.5pl3".
|
例 31-2Internal declaration of zend_module_entry.
|
For reference purposes, you can find a list of the macros involved in declared startup and shutdown functions in Table 31-3.
|
In our example, this structure is implemented as follows:
|
The generated name will be zend_info_ module (for example, zend_info_first_module).
|
表 31-3Macros to Declare Startup and Shutdown Functions
|
Prev
|
Now, in the following sections, read on about how to make use of PHP's internals to build powerful extensions.
|
If you compiled it into the PHP binary, omit the call to dl(), as the module's functionality is instantly available to your scripts.
|
For security reasons, you should not put your dynamic modules into publicly accessible directories. Even though it can be done and it simplifies testing, you should put them into a separate directory in production environments.
|
Even though it can be done and it simplifies testing, you should put them into a separate directory in production environments.
|
例 29-1A test file for first_module.so.
|
Calling this PHP file in your Web browser should give you the output shown in Figure 29-1.
|
Calling this PHP file in your Web browser should give you the output shown in 図29-1.
|
If required, the dynamic loadable module is loaded by calling the dl() function.
|
図 29-1Output of first_module.php.
|
You just built your first extension to PHP.
|
If you've gotten this far, congratulations! You just built your first extension to PHP.
|
For every array that you want to create, you need a new hash table handle, which will be stored in the ht member of the zval.value container.
|
Arrays are stored using Zend's internal hash tables, which can be accessed using the zend_hash_*() API. For every array that you want to create, you need a new hash table handle, which will be stored in the ht member of the zval.value container.
|
To add new elements to the array, you can use numerous functions, depending on what you want to do.
|
There's a whole API solely for the creation of arrays, which is extremely handy. To start a new array, you call array_init().
|
add_assoc_stringl( zval *array, char *key, char *str, uint length, int duplicate); ()
|
To add new elements to the array, you can use numerous functions, depending on what you want to do. 表33-1, 表33-2 and 表33-3 describe these functions. All functions return FAILURE on failure and SUCCESS on success.
|
add_index_stringl( zval *array, uint idx, char *str, uint length, int duplicate) ;()
|
表 33-1Zend's API for Associative Arrays
|
add_next_index_stringl( zval *array, char *str, uint length, int duplicate) ;()
|
表 33-2Zend's API for Indexed Arrays, Part 1
|
All these functions provide a handy abstraction to Zend 's internal hash API.
|
表 33-3Zend's API for Indexed Arrays, Part 2
|
Adding an element to an associative array.
|
例 33-3Adding an element to an associative array.
|
Adding an element to an indexed array.
|
例 33-4Adding an element to an indexed array.
|
zend_hash_next_index_insert( ht, zval **new_element, sizeof(zval *), NULL)
|
To emulate the functionality of add_next_index_*(), you can use this:
|
Prev
|
Tip: To avoid having to write new_array- value.ht every time, you can use HASH_OF(new_array), which is also recommended for compatibility and style reasons.
|
zval *new_bool; MAKE_STD_ZVAL(new_bool); new_bool - type = IS_BOOL; new_bool - value.lval = 1;
|
Booleans are created just like longs, but have the type IS_BOOL. Allowed values in lval are 0 and 1:
|
Constants are accessed without the typical dollar sign ($) prefix and are available in all scopes.
|
Zend supports the creation of true constants (as opposed to regular variables). Constants are accessed without the typical dollar sign ($) prefix and are available in all scopes. Examples include TRUE and FALSE, to name just two.
|
To create your own constants, you can use the macros in Table 33-6.
|
To create your own constants, you can use the macros in 表33-6. All the macros create a constant with the specified name and value.
|
You can also specify flags for each constant:
|
You can also specify flags for each constant:
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.