cvs: presentations / hacking-2003.xml /slides/extending-tidy ext10.xml ext11.xml ext12.xml ext13.xml ext14.xml ext15.xml ext16.xml ext17.xml
ext18.xml ext19.xml ext20.xml ext3.xml ext4.xml ext5.xml ext6.xml ext7.xml ext8.xml ext9.xml

From: Date: Fri, 25 Apr 2003 12:23:37 +0000
Subject: cvs: presentations / hacking-2003.xml /slides/extending-tidy ext10.xml ext11.xml ext12.xml ext13.xml ext14.xml ext15.xml ext16.xml ext17.xml
ext18.xml ext19.xml ext20.xml ext3.xml ext4.xml ext5.xml ext6.xml ext7.xml ext8.xml ext9.xml
Groups: php.pres 
Request: Send a blank email to pres+get-648@lists.php.net to get a copy of this message
jimw Fri Apr 25 08:23:37 2003 EDT Added files: /presentations/slides/extending-tidy ext10.xml ext11.xml ext12.xml ext13.xml ext14.xml ext15.xml ext16.xml ext17.xml ext18.xml ext19.xml ext20.xml ext3.xml ext4.xml ext5.xml ext6.xml ext7.xml ext8.xml ext9.xml Modified files: /presentations hacking-2003.xml Log: add new version of 'extending' slides that talk about tidy extension (will incorporate more examples from the extension to replace the generic ones as time permits)

Index: presentations/hacking-2003.xml diff -u presentations/hacking-2003.xml:1.1 presentations/hacking-2003.xml:1.2 --- presentations/hacking-2003.xml:1.1 Wed Apr 23 15:17:52 2003 +++ presentations/hacking-2003.xml Fri Apr 25 08:23:37 2003 @@ -18,11 +18,11 @@ <slide>slides/extending/ext1.xml</slide> <slide>slides/extending/ext2.xml</slide> -<slide>slides/extending/ext3.xml</slide> -<slide>slides/extending/ext4.xml</slide> -<slide>slides/extending/ext5.xml</slide> -<slide>slides/extending/ext6.xml</slide> -<slide>slides/extending/ext7.xml</slide> +<slide>slides/extending-tidy/ext3.xml</slide> +<slide>slides/extending-tidy/ext4.xml</slide> +<slide>slides/extending-tidy/ext5.xml</slide> +<slide>slides/extending-tidy/ext6.xml</slide> +<slide>slides/extending-tidy/ext7.xml</slide> <slide>slides/extending/ext8.xml</slide> <slide>slides/extending/ext9.xml</slide> <slide>slides/extending/ext10.xml</slide> Index: presentations/slides/extending-tidy/ext10.xml +++ presentations/slides/extending-tidy/ext10.xml <slide title="Parsing Parameters"> <blurb> In its simplest form you can call %ZEND_NUM_ARGS()% to check the number of parameters passed to your function. </blurb> <example type="c"><![CDATA[PHP_FUNCTION(count_args) { RETURN_LONG(ZEND_NUM_ARGS()); }]]></example> <blurb> You can use the %WRONG_PARAM_COUNT% macro to output a generic wrong parameter count warning. </blurb> <example type="c"><![CDATA[PHP_FUNCTION(my_func) { if(ZEND_NUM_ARGS() > 1) { WRONG_PARAM_COUNT; } RETURN_TRUE; }]]></example> </slide> Index: presentations/slides/extending-tidy/ext11.xml +++ presentations/slides/extending-tidy/ext11.xml <slide title="Advanced Parameter Parsing"> <blurb> To do real parameter handling, use the %zend_parse_parameters()% function. </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(return_arg) { int argc = ZEND_NUM_ARGS(); long arg; if (zend_parse_parameters(argc TSRMLS_CC, "l", &arg) == FAILURE) return; RETURN_LONG(arg); }]]></example> <blurb> You can make it an optional argument by putting a "%|%" in the argument format string. Like this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(return_arg) { int argc = ZEND_NUM_ARGS(); long arg; if (zend_parse_parameters(argc TSRMLS_CC, "|l", &arg) == FAILURE) return; if(argc) RETURN_LONG(arg) else RETURN_FALSE }]]></example> <blurb> Here are all the possible modifiers in the argument format string: </blurb> <example type="shell"><![CDATA['l' long (integer) 'd' double (floating point) 's' string 'b' boolean 'r' resource 'a' array 'o' any object 'O' specific object 'z' mixed]]></example> <blurb> So, for example, a function that takes a string, an integer, and array and an optional resource would look like this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(complex_func) { int argc = ZEND_NUM_ARGS(); char *str = NULL; int str_len; int res_id = -1; long arg; zval *arr = NULL; zval *res = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "sla|r", &str, &str_len, &arg, &arr, &res) == FAILURE) return; if (res) { ZEND_FETCH_RESOURCE(test_struct, test_le_struct *, &res, -1, "test", le_test); if(!test_struct) RETURN_FALSE; } /* do stuff ... */ }]]></example> </slide> Index: presentations/slides/extending-tidy/ext12.xml +++ presentations/slides/extending-tidy/ext12.xml <slide title="Accessing Array Parameters"> <blurb> If your function takes an array parameter. You can check it and access it like this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_arr) { int argc = ZEND_NUM_ARGS(); zval **tmp, *arr = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "a", &arr) == FAILURE) return; zend_hash_internal_pointer_reset(Z_ARRVAL_P(arr)); while (zend_hash_get_current_data(Z_ARRVAL_P(arr), (void **)&tmp) == SUCCESS) { convert_to_long_ex(tmp); php_printf("%ld\n",Z_LVAL_PP(tmp)); zend_hash_move_forward(Z_ARRVAL_P(arr)); } RETURN_TRUE; }]]></example> <blurb> Here the call to %zend_parse_parameters()% does the type check. If the user passes a non-array to this function s/he will get a message that looks like this: </blurb> <example fontsize="1em" type="shell"><![CDATA[foo.php(4) : Warning - my_arr() expects argument 1 to be array, integer given]]></example> <blurb> If you wanted to write a function that could take either an array of integers or perhaps a single integer and have your function figure it out automatically you do do it like this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_arr) { int argc = ZEND_NUM_ARGS(); zval **tmp, *arg = NULL; if (zend_parse_parameters(argc TSRMLS_CC, "z", &arg) == FAILURE) return; switch(Z_TYPE_P(arg)) { case IS_ARRAY: zend_hash_internal_pointer_reset(Z_ARRVAL_P(arg)); while (zend_hash_get_current_data(Z_ARRVAL_P(arg), (void **)&tmp) == SUCCESS) { convert_to_long_ex(tmp); php_printf("%ld\n",Z_LVAL_PP(tmp)); zend_hash_move_forward(Z_ARRVAL_P(arg)); } break; case IS_STRING: case IS_LONG: case IS_DOUBLE: convert_to_long_ex(&arg); php_printf("%ld\n",Z_LVAL_P(arg)); break; } RETURN_TRUE; }]]></example> <blurb> The possible types are: </blurb> <example type="c"><![CDATA[#define IS_NULL 0 #define IS_LONG 1 #define IS_DOUBLE 2 #define IS_STRING 3 #define IS_ARRAY 4 #define IS_OBJECT 5 #define IS_BOOL 6 #define IS_RESOURCE 7 #define IS_CONSTANT 8 #define IS_CONSTANT_ARRAY 9]]></example> </slide> Index: presentations/slides/extending-tidy/ext13.xml +++ presentations/slides/extending-tidy/ext13.xml <slide title="Returning Values"> <blurb> The simple types are returned using the following convenience macros: </blurb> <example fontsize="1em" type="c"><![CDATA[#define RETVAL_RESOURCE(l) ZVAL_RESOURCE(return_value, l) #define RETVAL_BOOL(b) ZVAL_BOOL(return_value, b) #define RETVAL_NULL() ZVAL_NULL(return_value) #define RETVAL_LONG(l) ZVAL_LONG(return_value, l) #define RETVAL_DOUBLE(d) ZVAL_DOUBLE(return_value, d) #define RETVAL_STRING(s, dup) ZVAL_STRING(return_value, s, dup) #define RETVAL_STRINGL(s, l, dup) ZVAL_STRINGL(return_value, s, l, dup) #define RETVAL_EMPTY_STRING() ZVAL_EMPTY_STRING(return_value) #define RETVAL_FALSE ZVAL_BOOL(return_value, 0) #define RETVAL_TRUE ZVAL_BOOL(return_value, 1) #define RETURN_RESOURCE(l) { RETVAL_RESOURCE(l); return; } #define RETURN_BOOL(b) { RETVAL_BOOL(b); return; } #define RETURN_NULL() { RETVAL_NULL(); return;} #define RETURN_LONG(l) { RETVAL_LONG(l); return; } #define RETURN_DOUBLE(d) { RETVAL_DOUBLE(d); return; } #define RETURN_STRING(s, dup) { RETVAL_STRING(s, dup); return; } #define RETURN_STRINGL(s, l, dup) { RETVAL_STRINGL(s, l, dup); return; } #define RETURN_EMPTY_STRING() { RETVAL_EMPTY_STRING(); return; } #define RETURN_FALSE { RETVAL_FALSE; return; } #define RETURN_TRUE { RETVAL_TRUE; return; }]]></example> <blurb> As you can see, returning a value from a function involves setting the special %return_value% zval to the return value and then just returning from the function call. At the C level, all functions return void. </blurb> <blurb> Simple %sum()% function: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_sum) { int argc = ZEND_NUM_ARGS(); long arg1; long arg2; if (zend_parse_parameters(argc TSRMLS_CC, "ll", &arg1, &arg2) == FAILURE) return; RETURN_LONG(arg1+arg2); }]]></example> <blurb> Here we uppercase every other character of a string. </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_upper) { char *str = NULL; int argc = ZEND_NUM_ARGS(); int str_len; char *p; int i; if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, &str_len) == FAILURE) return; for(p=str, i=0; *p; p++,i++) { if(i%2) *p = toupper(*p); } RETURN_STRINGL(str,str_len,1); }]]></example> </slide> Index: presentations/slides/extending-tidy/ext14.xml +++ presentations/slides/extending-tidy/ext14.xml <slide title="Returning an Array"> <blurb> Returning an array is a little bit more complex. Here we take a string and a count and return an array with the specified number of elements containing the given string. </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_array) { char *str = NULL; int argc = ZEND_NUM_ARGS(); int str_len; long count; int i; if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &count) == FAILURE) return; array_init(return_value); for(i=0; i<count; i++) { add_index_stringl(return_value, i, str, str_len, 1); } }]]></example> <blurb> So, to return an array you first call %array_init()% on the return_value zval and then populate the array by calling the various add_* functions. The functions available to populate an array are: </blurb> <example fontsize="1em" type="c"><![CDATA[add_index_long(zval *arg, uint idx, long n) add_index_null(zval *arg, uint idx) add_index_bool(zval *arg, uint idx, int b) add_index_resource(zval *arg, uint idx, int r) add_index_double(zval *arg, uint idx, double d) add_index_string(zval *arg, uint idx, char *str, int dup) add_index_stringl(zval *arg, uint idx, char *str, uint length, int dup) add_index_zval(zval *arg, uint index, zval *value)]]></example> <blurb> Recall that PHP supports the non-indexed array syntax. eg. </blurb> <example type="php"><![CDATA[<? $a[] = 1; ?>]]></example> <blurb> This same concept is available in the extension API using these functions: </blurb> <example fontsize="1em" type="c"><![CDATA[add_next_index_long(zval *arg, long n) add_next_index_null(zval *arg) add_next_index_bool(zval *arg, int b) add_next_index_resource(zval *arg, int r) add_next_index_double(zval *arg, double d) add_next_index_string(zval *arg, char *str, int dup) add_next_index_stringl(zval *arg, char *str, uint length, int dup) add_next_index_zval(zval *arg, zval *value)]]></example> <blurb> You can also create an array with non-numeric indices. Otherwise known as an associative array using these functions: </blurb> <example fontsize="1em" type="c"><![CDATA[add_assoc_long(zval *arg, char *key, long n) add_assoc_null(zval *arg, char *key) add_assoc_bool(zval *arg, char *key, int b) add_assoc_resource(zval *arg, char *key, int r) add_assoc_double(zval *arg, char *key, double d) add_assoc_string(zval *arg, char *key, char *str, int dup) add_assoc_stringl(zval *arg, char *key, char *str, uint length, int dup) add_assoc_zval(zval *arg, char *key, zval *value)]]></example> <blurb> To return a 2-dimensional array, you simply make new arrays and add them as elements of the top array. Like this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_array) { char *str = NULL; int argc = ZEND_NUM_ARGS(); int str_len; long count; int i; pval *tmp_arr; if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &count) == FAILURE) return; array_init(return_value); for(i=0; i<count; i++) { MAKE_STD_ZVAL(tmp_arr); array_init(tmp_arr); add_assoc_long_ex(tmp_arr, str, str_len, count+i); add_next_index_zval(return_value, tmp_arr); } }]]></example> <blurb> Note the use of %add_assoc_long_ex()% in the above example. If we know the length of the key, then this is slightly faster than calling %add_assoc_long()%. The macro definition shows why: </blurb> <example fontsize="1em" type="c"><![CDATA[int add_assoc_long_ex(zval *arg, char *key, uint key_len, long n); #define add_assoc_long(__arg, __key, __n) \\ add_assoc_long_ex(__arg, __key, strlen(__key)+1, __n)]]></example> </slide> Index: presentations/slides/extending-tidy/ext15.xml +++ presentations/slides/extending-tidy/ext15.xml <slide title="Object Factory Approach"> <blurb> This gets quite a bit more complex than returning simple types, or even arrays. The first thing to do is to declare an extension-wide global class ptr along with the methods available in this object you are creating. </blurb> <example fontsize="1.5em" type="c"><![CDATA[static zend_class_entry *my_class_entry_ptr; static zend_function_entry php_my_class_functions[] = { PHP_FE(add, NULL) PHP_FALIAS(del, my_del, NULL) PHP_FALIAS(list, my_list, NULL) {NULL, NULL, NULL} };]]></example> <blurb> Next, in our module_init function we initialize and register our class definition. </blurb> <example fontsize="1em" type="c"><![CDATA[PHP_MINIT_FUNCTION(test) { zend_class_entry my_class_entry; INIT_CLASS_ENTRY(my_class_entry, "my_class", php_my_class_functions); my_class_entry_ptr = zend_register_internal_class(&my_class_entry TSRMLS_CC); return SUCCESS; }]]></example> <blurb> And then we create our object factory function which will return an instantiated object. </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_object) { { char *fn = NULL; int argc = ZEND_NUM_ARGS(); int fn_len; long length; double price; zend_bool setting; if (zend_parse_parameters(argc TSRMLS_CC, "sbld", &fn, &fn_len, &setting, &length, &price) == FAILURE) return; object_init_ex(return_value, my_class_entry_ptr); add_property_stringl(return_value, "filename", fn, fn_len, 1); add_property_bool (return_value, "toggle", setting ? 0 : 1); add_property_long (return_value, "length", length); add_property_double (return_value, "price", price); }]]></example> <blurb> From user space it would be called like this: </blurb> <example type="php"><![CDATA[<?php $obj = my_object(); $obj->add(...); ?>]]></example> <blurb> If you want to have the more traditional class instantiation syntax and want to have a constructor run for it. eg. </blurb> <example type="php"><![CDATA[<?php $obj = new my_class(); ?>]]></example> <blurb> Then create a constructor function: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_class) { char *fn = NULL; int argc = ZEND_NUM_ARGS(); int fn_len; long length; double price; zend_bool setting; if (zend_parse_parameters(argc TSRMLS_CC, "sbld", &fn, &fn_len, &setting, &length, &price) == FAILURE) return; add_property_stringl(this_ptr, "filename", fn, fn_len, 1); add_property_bool(this_ptr, "toggle", setting?0:1); add_property_long(this_ptr, "length", length); add_property_double(this_ptr, "price", price); }]]></example> <blurb> Note the use of %this_ptr% here. </blurb> <blurb> If you want to access properties from within one of the methods, you can do this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[pval **tmp; if(zend_hash_find(HASH_OF(this_ptr), "my_property", 11, (void **)&tmp) == SUCCESS) { convert_to_string_ex(tmp); php_printf("my_property is set to %s\n", Z_STRVAL_PP(tmp)); }]]></example> </slide> Index: presentations/slides/extending-tidy/ext16.xml +++ presentations/slides/extending-tidy/ext16.xml <slide title="Setting and Accessing Global Variables"> <blurb> If you need extension-wide global variables, you can't just use standard C-style static variables because PHP needs to be thread-safe. To achieve this, globals are placed in a struct and passed around. To add global variables to your extension, first define them in your header file: </blurb> <example fontsize="1.5em" type="c"><![CDATA[ZEND_BEGIN_MODULE_GLOBALS(test) int some_integer; char *some_string; ZEND_END_MODULE_GLOBALS(test) /* shortcut macros */ #ifdef ZTS # define TEST_G(v) TSRMG(test_globals_id, zend_test_globals *, v) #else # define TEST_G(v) (test_globals.v) #endif]]></example> <blurb> Then in your .c file: </blurb> <example fontsize="1.5em" type="c"><![CDATA[ZEND_DECLARE_MODULE_GLOBALS(foo) static void php_foo_init_globals(zend_foo_globals *foo_globals) { foo_globals->some_integer = 0; foo_globals->some_string = NULL; }]]></example> <blurb> And in your MINIT function: </blurb> <example fontsize="1.5em" type="c"><![CDATA[ZEND_INIT_MODULE_GLOBALS(foo, php_foo_init_globals, NULL);]]></example> <blurb> Now, anywhere you need to get/set one of these variables, use %TEST_G(some_integer)%. This will only work if the test_globals struct is available in the function, of course. All the standard PHP functions will automatically have the globals struct available, but if you write your own utility functions you either have to call %TSRMLS_FETCH();% at the top of the function, or better yet, pass in the TSRM struct. Like this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[test_utility_function(my_arg TSRMLS_CC);]]></example> <blurb> And the function is declared using: </blurb> <example fontsize="1.5em" type="c"><![CDATA[static void test_utility_function(int my_arg TSRMLS_DC)]]></example> </slide> Index: presentations/slides/extending-tidy/ext17.xml +++ presentations/slides/extending-tidy/ext17.xml <slide title="SAPI Globals"> <blurb> SAPI is the Server abstraction API and you can access server-related global variables using the %SG()% macro. Include %SAPI.h% in your .c file to get the SG macro. You can then access the elements of the %sapi_globals_struct% which looks like this: </blurb> <example type="c"><![CDATA[typedef struct _sapi_globals_struct { void *server_context; sapi_request_info request_info; sapi_headers_struct sapi_headers; int read_post_bytes; unsigned char headers_sent; struct stat global_stat; char *default_mimetype; char *default_charset; HashTable *rfc1867_uploaded_files; long post_max_size; int options; } sapi_globals_struct;]]></example> <blurb> To access the default MIME type, you would use: </blurb> <example type="c">SG(default_mimetype)</example> <blurb> And to access the request_uri you would use: </blurb> <example type="c">SG(request_info).request_uri</example> <blurb title="Executor Globals"> These are run-time globals. The ones you are likely to be interested in are the %symbol_table% and %active_symbol_table% globals. For example, to access the user-space $foo variable from the current global context, you would do this: </blurb> <example fontsize="1.5em" type="c"><![CDATA[ pval **tmp; if(zend_hash_find(&EG(symbol_table), "foo", 4, (void **)&tmp) == SUCCESS) { RETURN_STRINGL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), 1); } else { RETURN_FALSE; }]]></example> </slide> Index: presentations/slides/extending-tidy/ext18.xml +++ presentations/slides/extending-tidy/ext18.xml <slide title="Adding php.ini Entries"> <blurb> Generally the extension-wide global variables are actually configuration options set in the %php.ini% file, or perhaps in the Apache %httpd.conf% file. Adding such configuration options for your extension is easy. To add a %test.my_ini_setting% configuration variable, first follow the instructions for creating a global variable from the last slide. Then in your .c file add: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_INI_BEGIN() STD_PHP_INI_ENTRY("test.some_integer", "0", PHP_INI_ALL, OnUpdateInt, some_integer, zend_test_globals, test_globals) PHP_INI_END()]]></example> <blurb> The arguments to STD_PHP_INI_ENTRY here are: </blurb> <list> <bullet>entry name</bullet> <bullet>entry value</bullet> <bullet>change permissions</bullet> <bullet>pointer to change notification handler</bullet> <bullet>corresponding global variable</bullet> <bullet>globals struct type</bullet> <bullet>globals struct</bullet> </list> <blurb> You can implement your own change notification handlers using %PHP_INI_MH()%, but here we will just use the default %OnUpdateInt% which basically just takes the string in the .ini file and does an atoi() on it. The other built-in handlers are %OnUpdateReal()%, %OnUpdateBool()%, %OnUpdateString()%, and %OnUpdateStringUnempty()%. </blurb> <blurb> After adding your STD_PHP_INI_ENTRY block to your .c file, in your MINIT function after the %ZEND_INIT_MODULE_GLOBALS()% add this: </blurb> <example type="c">REGISTER_INI_ENTRIES();</example> <blurb> And likewise, in your MSHUTDOWN function add: </blurb> <example type="c">UNREGISTER_INI_ENTRIES();</example> <blurb> And finally, you can optionally add this to your MINFO function: </blurb> <example type="c">DISPLAY_INI_ENTRIES();</example> </slide> Index: presentations/slides/extending-tidy/ext19.xml +++ presentations/slides/extending-tidy/ext19.xml <slide title="Memory Management"> <blurb> PHP has its own memory management functions which adds a safety net in case you forget to free things. It also helps catch overflows and other memory-related issues. The functions are exactly like their typical C counterparts: </blurb> <example type="c"><![CDATA[emalloc efree estrdup estrndup ecalloc erealloc]]></example> </slide> Index: presentations/slides/extending-tidy/ext20.xml +++ presentations/slides/extending-tidy/ext20.xml <slide title="Resources"> <blurb> A generic data container. Used to hold file handles, database connections, or anything else that needs to be initialized and then passed around for other functions to act on. To create a resource in your extension, first declare an extension-wide true global. We don't have to worry about thread-safety here. And then if your resource holds a complex datatype of some sort, typedef it as well at the top of your .c file (on in your header file): </blurb> <example fontsize="1.5em" type="c"><![CDATA[static int le_test; typedef struct _test_le_struct { char *name; long age; } test_le_struct;]]></example> <blurb> Next we need to create a resource destructor function. </blurb> <example fontsize="1.5em" type="c"><![CDATA[static void _php_free_test(zend_rsrc_list_entry *rsrc TSRMLS_DC) { test_le_struct *test_struct = (test_le_struct *)rsrc->ptr; efree(test_struct->name); efree(test_struct); }]]></example> <blurb> Then in our MINIT function we declare/initialize our resource: </blurb> <example fontsize="1.5em" type="c"><![CDATA[le_test = zend_register_list_destructors_ex(_php_free_test, NULL, "test", module_number);]]></example> <blurb> Then we might have a function that looks like this that returns a resource: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_init) { char *name = NULL; int name_len, age; test_le_struct *test_struct; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &name, &name_len, &age) == FAILURE) { return; } test_struct = emalloc(sizeof(test_le_struct)); test_struct->name = estrndup(name, name_len); test_struct->age = age; ZEND_REGISTER_RESOURCE(return_value, test_struct, le_test); }]]></example> <blurb> Followed by other functions that then take a resource as an argument and do something with it: </blurb> <example fontsize="1.5em" type="c"><![CDATA[PHP_FUNCTION(my_get) { test_le_struct *test_struct; pval *res; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) { return; } ZEND_FETCH_RESOURCE(test_struct, test_le_struct *, &res, -1, "test", le_test); if(!test_struct) RETURN_FALSE; array_init(return_value); add_assoc_string(return_value, "name", test_struct->name, 1); add_assoc_long(return_value, "age", test_struct->age); }]]></example> </slide> Index: presentations/slides/extending-tidy/ext3.xml +++ presentations/slides/extending-tidy/ext3.xml <slide title="Plan your Extension"> <blurb> Try to plan your extension before you start writing your code. A good way to do this is to write a sample PHP script that illustrates how your extension is going to be accessed from a PHP script. </blurb> <example title="HTML Tidy Sample Extension" type="php"><![CDATA[<?php $tidy = tidy_create(); tidy_set_option($tidy, "show-body-only", "yes"); tidy_parse_string("<b>foo <i>bar</i></b>"); tidy_clean_and_repair($tidy); tidy_output($tidy); ?>]]></example> </slide> Index: presentations/slides/extending-tidy/ext4.xml +++ presentations/slides/extending-tidy/ext4.xml <slide title="Define the functions"> <blurb>From your plan, generate a function definition file</blurb> <example type="shell" title="tidy.def"><![CDATA[resource tidy_create() Create a new Tidy document object void tidy_release(resource tdoc) Release an allocated Tidy document object string tidy_release_date() Get release date (version) for current library bool tidy_set_option(resource tdoc, string name, string value) Get release date (version) for current library int tidy_status(resource tdoc) Get status of current document int tidy_detected_html_version(resource tdoc) Detected HTML version: 0, 2, 3 or 4 bool tidy_detected_xhtml(resource tdoc) Input is XHTML? bool tidy_detected_generic_xml(resource tdoc) Input is generic XML (not HTML or XHTML)? int tidy_error_count(resource tdoc) Number of Tidy errors encountered. If > 0, output is suppressed unless TidyForceOutput is set. int tidy_warning_count(resource tdoc) Number of Tidy warnings encountered int tidy_access_warning_count(resource tdoc) Number of Tidy accessibility warnings encountered int tidy_config_error_count(resource tdoc) Number of Tidy configuration errors encountered int tidy_load_config(resource tdoc, string file [, string encoding]) Load a Tidy configuration file bool tidy_file_exists(string file) Whether a file exists (with tilde expansion) int tidy_set_character_encoding(resource tdoc, string encoding) Set the input/output character encoding for parsing markup int tidy_parse_file(resource tdoc, string filename) Parse markup from a file int tidy_parse_string(resource tdoc, string source) Parse markup from a string int tidy_clean_and_repair(resource tdoc) Execute configured cleanup and repair operations on parsed markup int tidy_run_diagnostics(resource tdoc) Run configured diagnostics on parsed and repaired markup (must call tidy_clean_and_repair() first) int tidy_output(resource tdoc [, string filename]) Output currently parsed document (to standard output or specified filename) void tidy_error_summary(resource tdoc) Write more complete information about errors void tidy_general_info(resource tdoc) Write more general information about markup]]></example> </slide> Index: presentations/slides/extending-tidy/ext5.xml +++ presentations/slides/extending-tidy/ext5.xml <slide title="Creating a skeleton extension"> <blurb> Run the *ext_skel* script which creates all the required skeleton files. </blurb> <example type="shell"><![CDATA[% cd php4/ext % ./ext_skel --extname=tidy --proto=tidy.def Creating directory tidy Creating basic files: config.m4 Makefile.in .cvsignore tidy.c php_tidy.h CREDITS EXPERIMENTAL tests/001.phpt tidy.php [done]. % ls tidy config.m4 CREDITS EXPERIMENTAL tidy.c tidy.php Makefile.in php_tidy.h tests/]]></example> <blurb title="Standalone or Embedded?"> At this point you have a choice on how to proceed. You can either develop your extension under the main PHP framework where you run the top-level *buildconf* script to include your extension in PHP's main configuration script, or you can develop your extension in a completely separate directory where you run the *phpize* script to populate your extension directory with all the required files. </blurb> <example title="Embedded" type="shell"><![CDATA[% cd tidy % vi config.m4 % cd ../.. % ./buildconf % ./configure --[with|enable]-tidy % make]]></example> <example title="Standalone" type="shell"><![CDATA[% mv tidy /tmp % cd /tmp/tidy % vi config.m4 % phpize % ./configure --[with|enable]-tidy % make]]></example> </slide> Index: presentations/slides/extending-tidy/ext6.xml +++ presentations/slides/extending-tidy/ext6.xml <slide title="Compiling"> <example title="Compiling the test skeleton extension" type="shell"><![CDATA[% cd /tmp/tidy % vi config.m4]]></example> <blurb title="The config.m4 file"> The *config.m4* file adds any extension-specific configuration options. If your extension relies on external libraries and header files, you need to define those in this file and uncomment the *--with-tidy* section. If your extension does not rely on any external libraries, uncomment the *--enable-tidy* section. </blurb> <example title="Configure, build and install" type="shell"><![CDATA[% ./configure % make % cp modules/tidy.so /usr/local/lib/php]]></example> <blurb title="Testing your extension"> The *ext_skel* script created a sample *tidy.php* script. A quick way to check if your extension is working is to edit this script and add *dl('tidy.so')* to the top. Then run it through your command-line PHP parser: </blurb> <example type="shell"><![CDATA[% php -q tidy.php Congratulations! You have successfully modified ext/tidy/config.m4. Module tidy is now compiled into PHP.]]></example> </slide> Index: presentations/slides/extending-tidy/ext7.xml +++ presentations/slides/extending-tidy/ext7.xml <slide title="Structure"> <blurb> The structure of a typical extension contains the following components: </blurb> <example title="includes" type="c"><![CDATA[#ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_tidy.h"]]></example> <example title="function_entry" type="c"><![CDATA[function_entry tidy_functions[] = { PHP_FE(confirm_tidy_compiled, NULL) {NULL, NULL, NULL} };]]></example> <example title="module_entry" type="c"><![CDATA[zend_module_entry tidy_module_entry = { "tidy", tidy_functions, PHP_MINIT(tidy), PHP_MSHUTDOWN(tidy), PHP_RINIT(tidy), PHP_RSHUTDOWN(tidy), PHP_MINFO(tidy), STANDARD_MODULE_PROPERTIES };]]></example> <example fontsize="1.5em" title="PHP_INI_BEGIN" type="c"><![CDATA[PHP_INI_BEGIN() STD_PHP_INI_ENTRY("tidy.value", "42", PHP_INI_ALL, OnUpdateInt, global_value, zend_tidy_globals, tidy_globals) STD_PHP_INI_ENTRY("tidy.string", "tidybar", PHP_INI_ALL, OnUpdateString, global_string, zend_tidy_globals, tidy_globals) PHP_INI_END()]]></example> <example title="PHP_MINIT_FUNCTION" type="c"><![CDATA[PHP_MINIT_FUNCTION(tidy) { REGISTER_INI_ENTRIES(); return SUCCESS; }]]></example> <example title="PHP_MSHUTDOWN_FUNCTION" type="c"><![CDATA[PHP_MSHUTDOWN_FUNCTION(tidy) { UNREGISTER_INI_ENTRIES(); return SUCCESS; }]]></example> <example title="PHP_RINIT_FUNCTION" type="c"><![CDATA[PHP_RINIT_FUNCTION(tidy) { return SUCCESS; }]]></example> <example title="PHP_RSHUTDOWN_FUNCTION" type="c"><![CDATA[PHP_RSHUTDOWN_FUNCTION(tidy) { return SUCCESS; }]]></example> <example fontsize="1.5em" title="PHP_MINFO_FUNCTION" type="c"><![CDATA[PHP_MINFO_FUNCTION(tidy) { php_info_print_table_start(); php_info_print_table_header(2, "tidy support", "enabled"); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); }]]></example> <example fontsize="1.5em" title="functions" type="c"><![CDATA[PHP_FUNCTION(confirm_tidy_compiled) { char *arg = NULL; int arg_len, len; char string[256]; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = sprintf(string, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "tidy", Z_STRVAL_PP(arg)); RETURN_STRINGL(string, len, 1); }]]></example> </slide> Index: presentations/slides/extending-tidy/ext8.xml +++ presentations/slides/extending-tidy/ext8.xml <slide title="the pval/zval base datatype"> <blurb> Data in PHP is stored in the pval or zval datatype. Two names for the same thing. They are defined like this: </blurb> <example type="c"><![CDATA[typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object obj; } zvalue_value; struct _zval_struct { zvalue_value value; /* value */ zend_uchar type; /* active type */ zend_uchar is_ref; zend_ushort refcount; }; typedef struct _zval_struct zval; typedef zval pval;]]></example> </slide> Index: presentations/slides/extending-tidy/ext9.xml +++ presentations/slides/extending-tidy/ext9.xml <slide title="Convenience Macros"> <example fontsize="1.5em" type="c"><![CDATA[#define Z_LVAL(zval) (zval).value.lval #define Z_BVAL(zval) ((zend_bool)(zval).value.lval) #define Z_DVAL(zval) (zval).value.dval #define Z_STRVAL(zval) (zval).value.str.val #define Z_STRLEN(zval) (zval).value.str.len #define Z_ARRVAL(zval) (zval).value.ht #define Z_OBJ(zval) &(zval).value.obj #define Z_OBJPROP(zval) (zval).value.obj.properties #define Z_OBJCE(zval) (zval).value.obj.ce #define Z_RESVAL(zval) (zval).value.lval #define Z_LVAL_P(zval_p) Z_LVAL(*zval_p) #define Z_BVAL_P(zval_p) Z_BVAL(*zval_p) #define Z_DVAL_P(zval_p) Z_DVAL(*zval_p) #define Z_STRVAL_P(zval_p) Z_STRVAL(*zval_p) #define Z_STRLEN_P(zval_p) Z_STRLEN(*zval_p) #define Z_ARRVAL_P(zval_p) Z_ARRVAL(*zval_p) #define Z_OBJ_P(zval_p) Z_OBJ(*zval_p) #define Z_OBJPROP_P(zval_p) Z_OBJPROP(*zval_p) #define Z_OBJCE_P(zval_p) Z_OBJCE(*zval_p) #define Z_RESVAL_P(zval_p) Z_RESVAL(*zval_p) #define Z_LVAL_PP(zval_pp) Z_LVAL(**zval_pp) #define Z_BVAL_PP(zval_pp) Z_BVAL(**zval_pp) #define Z_DVAL_PP(zval_pp) Z_DVAL(**zval_pp) #define Z_STRVAL_PP(zval_pp) Z_STRVAL(**zval_pp) #define Z_STRLEN_PP(zval_pp) Z_STRLEN(**zval_pp) #define Z_ARRVAL_PP(zval_pp) Z_ARRVAL(**zval_pp) #define Z_OBJ_PP(zval_pp) Z_OBJ(**zval_pp) #define Z_OBJPROP_PP(zval_pp) Z_OBJPROP(**zval_pp) #define Z_OBJCE_PP(zval_pp) Z_OBJCE(**zval_pp) #define Z_RESVAL_PP(zval_pp) Z_RESVAL(**zval_pp)','100%')]]></example> </slide>
« previous php.pres (#648) next »