CHAPTER 15 – Accessing a Resource
To access a resource, you need to use the fol- lowing macro (see Table 15.6 for an explanation of its arguments): ZEND_FETCH_RESOURCE(rsrc, rsrc_type, passed_id, default_id, resource_type_name, resource_type);
Table 15.6 ZEND_FETCH_RESOURCE Macro Arguments Parameter Meaning rsrc Variable that is assigned the resource value. It has to be of the same type as the resource. rsrc_type Type of rsrc that will be used to cast the resource internally to the correct type. passed_id The resource value to look for (as a zval **). default_id If this value is not 1, this id is taken. It is used for implementing a default for the resource. resource_type_name A short type name for your resource which is used in error messages. resource_type The resource type id of the registered resource. Using this macro, we can now implement file_eof(): PHP_FUNCTION(file_eof) { int argc = ZEND_NUM_ARGS(); zval *filehandle = NULL; FILE *fp;
if (zend_parse_parameters(argc TSRMLS_CC, "r", &filehandle) ==FAILURE) { return; }
ZEND_FETCH_RESOURCE(fp, FILE *, &filehandle, -1, "standard-c file",le_myfile); if (fp == NULL) { RETURN_FALSE; } if (feof(fp) <= 0) { /* Return eof also if there was an error */ RETURN_TRUE; } RETURN_FALSE; }