In PHP, we often use resource type variables. For example, MySQL connection and file handle.
These variables cannot be expressed by scalar. How does one link the resource variables in PHP with the resources in C language in the Zend kernel?
I. Use of resource variables in PHP
$fp = fopen("test.txt", "rw");var_dump($fp);fclose($fp);
Printed result: Resource (5) of type (Stream)
Number 5: indicates that the resource ID is 5.
Stream: resource type name.
Ii. Resource ID
In the kernel, the registered resource variables are stored in a hashtable, and the key in the hashtable where the resource is located is used as the resource ID.
In fact, the resource variable in PHP is actually stored as an integer, and the corresponding resource in hashtable is found through this ID.
#define Z_RESVAL(zval)(zval).value.lval#define Z_RESVAL_P(zval)Z_RESVAL(*zval)#define Z_RESVAL_PP(zval)Z_RESVAL(**zval)
The macro above is the API in the kernel that Ze assigns values to resource variables. It can be seen that it is indeed a value for integer variables.
Iii. Resource Type name
To differentiate resource types, we need to define the type name for the resource we have defined.
# Define my_res_name "my_resource" // resource type name. When PHP prints the resource variable through var_dump, the name static int my_resource_descriptor is displayed. zend_minit_function (JINYONG) {condition = empty (null, null, my_res_name, module_number); // register a new resource type with the kernel}
Zend_minit_function (JINYONG) will execute all the extended zend_minit_functions when PHP is loaded into the memory as SAPI (for example, Apache mod_php5 extension.
Here, JINYONG is the name of the current extension. For example, the extension name is JINYONG.
For ease of understanding, we will regard it as a new resource type that will be registered with the Kernel During the initialization of the extension.
4. Create resource variables
The resource type has been registered successfully, and a distinguished type name is also defined for the resource. Now you can use this resource variable.
Implement the fopen function in PHP:
Php_function (my_fopen) {zval * res; char * filename, * mode; int filename_strlen, mode_strlen; file * FP; If (convert (zend_num_args tsrmls_cc, "s | s ", & filename, & filename_strlen, & mode, & mode_strlen) = failure) {return_false;} // The parameter validity verification fp = fopen (filename, mode) is omitted here ); zend_register_resource (Res, FP, my_resource_descriptor); // register resource variables in the global variable & EG (regular_list) and assign the ID of the corresponding hashtable to resreturn_resource (RES ); // return resource variables to PhP}
The function named my_fopen in PHP is defined here. My_fopen (string $ file_name, string $ Mode)
Implement the fclose function in PHP:
Php_function (my_fclose) {zval * res; file * FP; If (zend_parse_parameters (zend_num_args tsrms_cc, "r", & res) = failure) {return_false ;} if (z_type_p (RES) = is_resource) {// determine whether the variable type is resource type zend_hash_index_del (& EG (regular_list), z_resval_p (RES )); // EG is similar to $ _ globals in PHP. Delete the resource of the corresponding ID in the global resource variable regular_list} else {php_error_docref (null tsrmls_cc, e_warning, "the parameter must be a resource type variable"); return_false;} return_true ;}
Defines the function named my_fclose in PHP. My_fclose ($ Resource)
5. Compile and install extensions and restart PHP-FPM or mod_php5.
6. Use methods in custom extensions in PHP
my_fwrite($fp, "aaTest");var_dump($fp);my_fclose($fp);var_dump($fp);
You can enable or disable resources normally.
7. We often use databases to connect resources and file handle resources in PHP. But they do not require manual release or memory leakage. How can this problem be achieved?
My_resource_descriptor = zend_register_list_destructors_ex (null, null, my_res_name, module_number); // register a new resource type with the kernel
Return to the initial registered resource type and see the first parameter of zend_register_list_destructors_ex. this parameter is the pointer of the destructor.
To enable automatic release, you only need to define the Destructor and pass the function pointer.
Let's look at another question:
$ Fp = fopen ("test.txt", "RW"); var_dump ($ FP); // fclose ($ FP); Do not use fclose to release resources unset ($ FP) here ); // instead, use unset to release the $ FP variable. But the $ FP corresponding to the true open file resource handle resources will never be released until mod_php5 or PHP-FPM restarts // you can see that the necessity to define the Destructor when registering the resource type
Define destructor:
Static void php_myres_dtor (zend_rsrc_list_entry * rsrc tsrmls_dc) {// when the Destructor is called, the file * fp = (File *) rsrc-> PTR parameter of the current resource variable is accepted; fclose (FP);} zend_minit_function (JINYONG) {my_resource_descriptor = zend_register_list_destructors_ex (php_myres_dtor, null, my_res_name, module_number );}
In PHP, the so-called resource variables are actually stored by storing integer values. Find the corresponding pointer in the kernel global resource Variable list EG (regular_list) and perform corresponding operations. The PHP resource variable does not need to worry about issues similar to the MySQL connection not released because the Destructor are defined in the extension to help automatically release the resource.