To remove a resource, you usually want to
use the following macro:
int zend_list_delete(int id)
The macro is passed the id of the resource, and returns either SUCCESS or
FAILURE. If the resource exists, prior to removing it from the Zend resource list,
it will call the registered destructor for the resource type. Therefore, in our
example, you don’t have to obtain the file pointer and fclose() it before remov-
ing the resource, but you can just go ahead and delete it.
Using this macro, we can now implement file_close():
PHP_FUNCTION(file_close)
{
int argc = ZEND_NUM_ARGS();
zval *filehandle = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, “r”, &filehandle) ==
FAILURE) {
return;
}
if (zend_list_delete(Z_RESVAL_P(filehandle)) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
You must be asking yourself what Z_RESVAL_P() does. When we retrieve
the resource from the argument list using zend_parse_parameters(), we receive
it in the form of a zval. To access the resource id, we use the Z_RESVAL_P()
macro, and then pass it to zend_list_delete().
A whole family of macros aid in accessing values stored in zval values
(see Table 15.7 for a list of macros). Although zend_parse_parameters() in most
cases returns the values as the corresponding C type, you might want to deal
with a zval directly, including in the case of resources.
Table 15.7 zval Accessor Macros
Macros
Used to Access
C Type
Z_LVAL, Z_LVAL_P,
Integer value
Long
Z_LVAL_PP
Z_BVAL, Z_BVAL_P,
Boolean value
zend_bool
Z_BVAL_PP
Z_DVAL, Z_DVAL_P,
Floating-point value
double
Z_DVAL_PP
Z_STRVAL, Z_STRVAL_P,
String value
char *
Z_STRVAL_PP
Z_STRLEN, Z_STRLEN_P,
String length
int
Z_STRLEN_PP
Z_RESVAL, Z_RESVAL_P,
Resource value
Long
Z_RESVAL_PP
Z_ARRVAL, Z_ARRVAL_P,
Associative array
HashTable *
Z_ARRVAL_PP
Table 15.7 zval Accessor Macros
Macros
Used to Access
C Type
Z_TYPE, Z_TYPE_P,
The zval’s type
Enumeration (IS_NULL, IS_LONG,
Z_TYPE_PP
IS_DOUBLE, IS_STRING, IS_ARRAY,
IS_OBJECT, IS_BOOL, IS_RESOURCE)
Z_OBJPROP,
The object’s properties
HashTable *
Z_OBJPROP_P,
hash (won’t be covered
Z_OBJPROP_PP
in this chapter).
Z_OBJCE, Z_OBJCE_P,
The object’s class infor-
zend_class_entry
Z_OBJCE_PP
mation (won’t be covered
in this chapter).
