Advanced PHP Programming- P12
Số trang: 50
Loại file: pdf
Dung lượng: 523.88 KB
Lượt xem: 11
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu advanced php programming- p12, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Advanced PHP Programming- P12528 Chapter 21 Extending PHP: Part I In most functions, you are handed a resource handle zval, and you need to extract the actual resource for it. Fortunately, doing so is very easy. If you are looking in a single list, you can use the following macro: ZEND_FETCH_RESOURCE(void *rsrc_struct, rsrc_struct_type, zval **zval_id, int default_id, char *name, int rsrc_list); These are the arguments of ZEND_FETCH_RESOURCE(): nrsrc_struct is the actual pointer you want the resource data to be stored in. nrsrc_struct_type is the type of struct the resource is (for example, FILE *). nzval_id is a zval of resource type that contains the resource ID. n default_id is an integer that specifies the default resource to use. A common use for this is to store the last accessed resource ID in an extension’s globals.Then, if a function that requires a resource does not have one passed to it, it simply uses the last resource ID. If -1 is used, no default is attempted. n name is a character string that is used to identify the resource you were seeking. This string is used only in information warning messages and has no technical purpose. n rsrc_list is the list that should be searched for the resource. If the resource fetch fails, a warning is generated, and the current function returns NULL. The following is the function pfgets(),which reads a line from a file resource creat- ed by pfopen(): PHP_FUNCTION(pfgets) { char *out; int length = 1024; zval *rsrc; FILE *fh; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “r|l”, &rsrc, &length) == FAILURE) { return; } ZEND_FETCH_RESOURCE(fh, FILE *, rsrc, -1, “Persistent File Handle”, persist); out = (char *) emalloc(length); fgets(out, length, fh); RETURN_STRING(out, 0); } Extension Basics 529Returning ErrorsGenerating procedural errors in extension code is almost identical to generating errors inPHP. Instead of calling trigger_error() in PHP, you can use zend_error() in C.zend_error() has the following API:zend_error(int error_type, char *fmt, ...);error_type is the full range of errors enumerated in Chapter 3, “Error Handling.”Otherwise, the API is identical to the printf() family of functions.The following func-tion generates a warning:zend_error(E_WARNING, “Hey this is a warning”);Remember that if you use E_ERROR, the error is fatal, and script execution is stopped.(Chapter 23, “Writing SAPIs and Extending the Zend Engine,” describes how to over-ride this behavior). Throwing exceptions is covered in detail in Chapter 22, which looks at object-ori-ented extensions in detail.Using Module HooksIn addition to enabling you to define and export function definitions, PHP also givesextensions the ability to run code in response to certain events in the PHP runtime.These events include the following: n Module startup n Module shutdown n Request startup n Request shutdown n phpinfo registrationWhen you create a module, one of the required components is zend_module_entry,which looks like this:zend_module_entry example_module_entry = { STANDARD_MODULE_HEADER, “example”, example_functions, PHP_MINIT(example), PHP_MSHUTDOWN(example), PHP_RINIT(example), PHP_RSHUTDOWN(example), PHP_MINFO(example), VERSION, STANDARD_MODULE_PROPERTIES};530 Chapter 21 Extending PHP: Part I The third member of this structure, example_functions, specifies the array of functions that will be registered by the extension.The rest of the structure declares the callbacks that will be executed by the various module hooks. Module Startup and Shutdown An extension’s module initialization and shutdown hooks are called when the extension is loaded and unloaded, respectively. For most extensions (those that are either compiled statically into PHP or loaded via an INI setting), module initialization happens once, at server startup. Module shutdown is similarly called during server shutdown. In the Apache 1.3 (or Apache 2 prefork MPM), this hook is called before any children are forked off.Thus, it is an ideal place to create or initialize any sort of global or shared resource, and it’s a poor place to initialize any resource that cannot be shared between processes. The module initialization hook is registered via the following function: PHP_MINIT_FUNCTION(example) { return SUCCESS; } In general, module initialization is the ideal place ...
Nội dung trích xuất từ tài liệu:
Advanced PHP Programming- P12528 Chapter 21 Extending PHP: Part I In most functions, you are handed a resource handle zval, and you need to extract the actual resource for it. Fortunately, doing so is very easy. If you are looking in a single list, you can use the following macro: ZEND_FETCH_RESOURCE(void *rsrc_struct, rsrc_struct_type, zval **zval_id, int default_id, char *name, int rsrc_list); These are the arguments of ZEND_FETCH_RESOURCE(): nrsrc_struct is the actual pointer you want the resource data to be stored in. nrsrc_struct_type is the type of struct the resource is (for example, FILE *). nzval_id is a zval of resource type that contains the resource ID. n default_id is an integer that specifies the default resource to use. A common use for this is to store the last accessed resource ID in an extension’s globals.Then, if a function that requires a resource does not have one passed to it, it simply uses the last resource ID. If -1 is used, no default is attempted. n name is a character string that is used to identify the resource you were seeking. This string is used only in information warning messages and has no technical purpose. n rsrc_list is the list that should be searched for the resource. If the resource fetch fails, a warning is generated, and the current function returns NULL. The following is the function pfgets(),which reads a line from a file resource creat- ed by pfopen(): PHP_FUNCTION(pfgets) { char *out; int length = 1024; zval *rsrc; FILE *fh; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “r|l”, &rsrc, &length) == FAILURE) { return; } ZEND_FETCH_RESOURCE(fh, FILE *, rsrc, -1, “Persistent File Handle”, persist); out = (char *) emalloc(length); fgets(out, length, fh); RETURN_STRING(out, 0); } Extension Basics 529Returning ErrorsGenerating procedural errors in extension code is almost identical to generating errors inPHP. Instead of calling trigger_error() in PHP, you can use zend_error() in C.zend_error() has the following API:zend_error(int error_type, char *fmt, ...);error_type is the full range of errors enumerated in Chapter 3, “Error Handling.”Otherwise, the API is identical to the printf() family of functions.The following func-tion generates a warning:zend_error(E_WARNING, “Hey this is a warning”);Remember that if you use E_ERROR, the error is fatal, and script execution is stopped.(Chapter 23, “Writing SAPIs and Extending the Zend Engine,” describes how to over-ride this behavior). Throwing exceptions is covered in detail in Chapter 22, which looks at object-ori-ented extensions in detail.Using Module HooksIn addition to enabling you to define and export function definitions, PHP also givesextensions the ability to run code in response to certain events in the PHP runtime.These events include the following: n Module startup n Module shutdown n Request startup n Request shutdown n phpinfo registrationWhen you create a module, one of the required components is zend_module_entry,which looks like this:zend_module_entry example_module_entry = { STANDARD_MODULE_HEADER, “example”, example_functions, PHP_MINIT(example), PHP_MSHUTDOWN(example), PHP_RINIT(example), PHP_RSHUTDOWN(example), PHP_MINFO(example), VERSION, STANDARD_MODULE_PROPERTIES};530 Chapter 21 Extending PHP: Part I The third member of this structure, example_functions, specifies the array of functions that will be registered by the extension.The rest of the structure declares the callbacks that will be executed by the various module hooks. Module Startup and Shutdown An extension’s module initialization and shutdown hooks are called when the extension is loaded and unloaded, respectively. For most extensions (those that are either compiled statically into PHP or loaded via an INI setting), module initialization happens once, at server startup. Module shutdown is similarly called during server shutdown. In the Apache 1.3 (or Apache 2 prefork MPM), this hook is called before any children are forked off.Thus, it is an ideal place to create or initialize any sort of global or shared resource, and it’s a poor place to initialize any resource that cannot be shared between processes. The module initialization hook is registered via the following function: PHP_MINIT_FUNCTION(example) { return SUCCESS; } In general, module initialization is the ideal place ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
24 trang 354 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 303 0 0 -
74 trang 296 0 0
-
96 trang 293 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0