Summary of PHP Extension development

Source: Internet
Author: User

1. Variable operation set Variable zval_* series function;        Example: Zval t;    Zval_string (T, "10", 2);        Get variable z_* series function get variable pointer z_*_p series function get pointer to variable pointer Z_*_PP series function Example: Z_strval (t);    Z_strlen (t);    Variable type conversion convert_to_* Series Function Example: CONVERT_TO_LONG_EX (t); Gets the variable type Z_type z_type_p z_type_pp can be proportional to the Is_* series constants: if (Z_type_p (filehandle)!=is_long) php_printf (    "This is long type!"); Application variable Memory General Alloc_zval (ZV), Make_std_zval (ZV) macro, run out of release Free_zval (ZV) and other macros to release the variable Memory zval_dtor (zval) zval_ptr_dtor (&zval);        Free_zval (Zval); Note: Free_zval only releases the ZVAL structure inside the body, and Zval_dtor releases the Zval variable and the value pointer memory print variable php_printf ("Hi function!"); Return the output stream, you can use Php_write () to continue writing the string variable generation function spprintf (char **pbuf, size_t max_len, const char *format, ...);    Run out of release Efree (); Request memory and copy a copy of the string Estrdup (&a) Estrndup (&a, 10); Variable Memory Request: Emalloc (size) efree (PTR) do not use malloc unless you can explicitly release 2. Global variable Auxiliary macro: EG (v);//global variable executor_globals such as $_globals[eg (Symbol_ta BLE), Address: EG (active_symbol_table) PG (v);//core variable php_core_globals such as: $_get $_post. PG (http_globals) [Track_vars_*],ini information SG (v);//SAPI variable request data sapi_globals_struct such as: HTTP RAW request Variable Sapi_request_info CG (v);//      Compiler variable compiler_globals can get function table, class table EX (v);//Current execution data zend_execute_data can get to the currently executing function, class, opcode etc OG (v);//Output variable Output_globals  3. Functions, divided into 5 classes zend_*_function The following is a C definition built-in function function parameter description for C:int HT, Zval *return_value, Zval **return_value_ptr, Zval *this_ptr, int return_value_used HT parameter number, but cannot be used directly, the Zend_num_args () to get the Return_value function return Zval return_        VALUE_PTR returns a pointer to a value, but wants to control the contents of the return value, not directly returning the return value when used, such as a parameter reference implementation.  This_ptr if the current method is a method of an object, this value points to the object that the method points to, cannot be used directly, uses the Getthis () function return_value_used The return value has no used example: int argc =        Zend_num_args ();        Char *str=null;        int strlen; Note: The string argument is a pointer to the pointer length of the shaped pointer if (zend_parse_parAmeters (argc tsrmls_cc, "S|s", &str,&strlen) {==failure) {return; For information about returning the modified return_value_ptr pointer to the ALLOC_ZVAL_* macro to request memory, refer to: HTTP://WWW.WALU.CC/PHPBOOK/6.1.MD returned macro Retu Rn_* series functions, Resources with Zend_register_resource return_zval ();  Returns the ZVAL structure if it is the requested memory, Return_zval (Zv, copy, dtor) copy 0 Dtor 0 if it is a stack memory return_zval (zv, copy, dtor) copy 1 dtor 04. Array and hash table 1. Zvalue_value is a union array when Hashtable 2. The Hashtable table has a pair of corresponding processing functions: Zend_hash_* series function 3. The hashtable on the array is generally not directly manipulated, and is handled by the Add_* series function 4. Hashtable memory application alloc_hashtable such as macro processing, the release free_hashtable 5. Array initialization is also not handled directly with Hashtable, using Array_init 5. Resource Variable 1. Resource variables need to be used to get a resource list parameter: 1. Request a resource, each request clears the list of resources for this request 2. Persistent resource, which is permanently saved after the service is started 3. Display resource Name 4. Internal management parameters, ignoring le_myfile    = ZEND_REGISTER_LIST_DESTRUCTORS_EX (Myfile_dtor,null, "Standard-c-file", Module_number);         static void Myfile_dtor (Zend_rsrc_list_entry *rsrc tsrmls_dc) {File *FP = (file *) rsrc->ptr;//transforms the specified type for easy operation later    Fclose (FP); } 2.  Register the resource in the resource list and set the Return_value  int R=zend_register_resource (Return_value, FP, le_myfile);  3. Query resource filehandle for resource Zval zend_fetch_resource (FP, FILE *,&filehandle,-1, "Standard-c-file", le_myfile);    if (fp = = NULL) {return_false;    } 4. Delete the resource filehandle to the resource Zval if (Zend_list_delete (Z_resval_p (filehandle)) = = FAILURE) {return_false; } 5. Global function Zend_function_entry test_functions[] = {//parameter: function name Zend_fe (Myecho, null) {NULL,    NULL, NULL}}; Register to module Zend_module_entry php_test_module_entry = {#if zend_module_api_no >= 20010901 standard_module_head        ER, #endif "My test ext",//Module name test_functions,//function list null, NULL, NULL, NULL, Php_minfo (Php_test),//module introduction #if zend_module_api_no >= 20010901 "1.6.0",//version #endif Standard_    Module_properties}; function implementation, refer to 3. function section php_function (myecho) {} user function call: Parameter: function_table function table, global from&CG(function_table) Gets the object, if the function is a method of an object, there is no NULL function name return value stored zval pointer, parameter number parameter array Call_user_ function (HashTable *function_table, Zval **object_pp, Zval *function_name, Zval *retval_ptr, Zend_uint Param_count, Zval     *params[] tsrmls_dc);     6. Class implementation, structure: Zend_class_entry two kinds of zend_internal_class and Zend_user_class//access control: ZEND_ACC_* Series macro//class method definition//Parameters: Class Name Method name    Zend_method (MyClass, Public_method) {php_printf ("Hi function!"); }//Register to function list static zend_function_entry myclass_method[]={//Parameters: Class Name Method name static method plus Zend_acc_static Zend_me (my    Class, Public_method, NULL, zend_acc_public) {null,null,null}};    Zend_class_entry CE;    Zend_class_entry *myclass_ce;    Parameters: Initial Structure class name method list Init_class_entry (CE, "MyClass", Myclass_method); This method generates a zend_class_entry based on the CE copy and mounts to the class list Zend_register_internal_class (&ce TSRMLS_CC); Add a class's Properties//Parameters: Class Pointer property Name property name Length property access rights: Zend_declare_property_* series function Zend_declare_property_null (Myclass_ce, "Public_var        ", strlen (" Public_var "), Zend_acc_public tsrmls_cc); Define class constants://Parameters: Class Pointer property Name property name Length property access rights: Zend_declare_class_constant* series function Zend_declare_class_constant_null (Myclass_ce,        "AAA", 3 TSRMLS_DC);             Read class static properties Zend_api zval *zend_read_static_property (zend_class_entry *scope, Char *name,    int Name_length,//property does not exist whether to throw notice zend_bool silent tsrmls_dc);             Update class static Properties Zend_api int Zend_update_static_property (zend_class_entry *scope, Char *name,    int name_length, zval *value tsrmls_dc); Other packaged shortcut functions zend_update_static_property_* 7. The interface structure is also: zend_class_entry static zend_function_entry i_myinterface_method[ ]={//Note that NULL here refers to Arg_info, which can be referred to as: parameter Arg_info definition zend_abstract_me (i_myinterface, hello, NULL)        {Null,null,null}};    Definition and initialization of zend_class_entry CE;    Init_class_entry (CE, "i_myinterface", I_myinterface_method); I_myinterface_ce = Zend_register_internal_interface (&cetsrmls_cc) 8. Inheritance and interface implementation 1. Implement interface: After writing the class, the interface is added: Parameters: Class pointer, implement interface number, interface pointer ... zend_class_implements (class_c    e tsrmls_cc,1,myinterface_ce); 2. Implementing inheritance: Registering classes using: ZEND_REGISTER_INTERNAL_CLASS_EX parameter: class pointer parent class pointer parent class name MYCLASS_CE = Zend_register_internal _CLASS_EX (&ce, Parent_class_ce, "Parent_class" TSRMLS_CC); 8. Object Zend_object_value structure: typedef struct _ZEND_OBJECT_VALUE {//u        nsigned int type, EG (objects_store). Object_buckets index Zend_object_handle handle;    Zend_object_handlers *handlers;    } Zend_object_value;    Created object: Zval *obj;    Make_std_zval (obj);    OBJECT_INIT_EX (obj, zend_class_entry class name); Object_init (obj) null object Read properties: Zend_api zval *zend_read_property (zend_class_entry *scope, Zval *object    , char *name, int name_length,//property does not exist whether to throw notice zend_bool silent tsrmls_dc); Update properties: Zend_api int Zend_update_static_property (zend_class_entry *scope, char *name, int name_le    Ngth, Zval *value tsrmls_dc); Other packaged shortcut functions zend_update_property_* call method: Zend_api zval* Zend_call_method (zval **object_pp, Zend_class_entry *obj_c E, zend_function **fn_proxy, const char *function_name, int function_name_len, zval **retval_ptr_ptr, int paRam_count, zval* arg1, zval* arg2 tsrmls_dc); Zend_call_method_with_0_params (obj, Obj_ce, Fn_proxy, Function_name, retval) Zend_call_method_with_1_params (&this_zval  , Myclass_ce,null, "Hello", NULL); 9. Common Structure and Macro interpretation: PHP_FN (name) function name generation PHP_MN (name) method name generation Zend_named_function (name) generates the specified name.    Functions are generally declared with the php_function (name) function Zend_method (classname, Name) class method declaration Zend_function_entry array is used to declare a member function of a module or class Generate Zend_function_entry elements supporting macros://Register global functions//Parameters: function Name parameter Php_fe (name, arg_info)//Register class function//        Parameters: Class name Method name parameter access control Php_me (classname, name, Arg_info, flags)//Register abstract functions for classes, classes and interfaces use this//parameter: Class name Method Name parameter Php_abstract_me (classname, Name, Arg_info) 10. The definition of the parameter arg_info generally does not need to define parameters, unless you want PHP to check the correctness of the parameters, such as the interface method to generate ARG    _INFO macro, take Arg_say_hello for example://Parameter: Zend_arg_info name, parameter at least first n zend_begin_arg_info (Arg_say_hello, 0)//parameter: Whether set to reference, parameter name Zend_arg_* Series functions can also declare objects such as Zend_arg_info (0, name) zend_end_arg_info ()//Other introduction//name parameter name//name_len parameter Name string length//class_name specifies the class name//class_name_len class name string length//array_type_hint identifies whether the parameter type is an array when the parameter type is a class//allow_n Ull is allowed to be set to null//pasS_by_ref is set as a reference, that is, using the & operator//return_reference the identity function will override the Return_value_ptr, and then introduce the function return value after the introduction//required_num_args When the set function is called, the pass parameter is at least the first n, and when set to-1, all arguments must be passed

Summary of PHP Extension development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.