Php: detailed description of resource data type instances

Source: Internet
Author: User
The resource data type is introduced by PHP4. A resource is a special variable type that stores a reference to an external resource, such as opening a file, connecting to a database, and drawing a canvas area. Resources are created and used through specialized functions. What is a resource data type?

The resource data type is introduced by PHP4. A resource is a special variable type that stores a reference to an external resource, such as opening a file, connecting to a database, and drawing a canvas area.

Resources are created and used through specialized functions.

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.

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.

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.

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 res RETURN_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)


Using 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.

Release resources

Because the PHP4 Zend Engine introduces a resource counting system, it can automatically detect that a resource is no longer referenced (the same as Java ). In this case, all external resources used by the resource are released by the garbage collection system. Therefore, some free-result functions are rarely used to manually release the memory.

Note: persistent database connections are special and will not be damaged by the garbage collection system.

In the next section, we will explain "null" in two special data types )".

The above is php: detailed description of resource data type instances. For more information, see other related articles in the first PHP community!

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.