CHAPTER 15 – Writing Your First Resource-Enabled PHP Function

Implementing file_open() should now be easy, and it should look as follows: PHP_FUNCTION(file_open) { char *filename = NULL; char *mode = NULL; int argc = ZEND_NUM_ARGS(); int filename_len; int mode_len; FILE *fp; if (zend_parse_parameters(argc TSRMLS_CC, "ss", &filename, &filename_len, &mode, &mode_len) == FAILURE) { return; } fp = VCWD_FOPEN(filename, mode); if (fp == NULL) { RETURN_FALSE; } ZEND_REGISTER_RESOURCE(return_value, fp, le_myfile); } You might notice that the first argument to the resource registration macro is a variable called return_value, which has appeared out of nowhere. This variable is automatically defined by the extension framework and is a zval * to the function's return value. The previously discussed macros, which affect the return value such as RETURN_LONG() and RETVAL_BOOL(), actually change the value of return_value. Therefore, it is easy to guess that the code registers our acquired file pointer fp and sets the return_value to the regis- tered resource.

Post Comment
Login to post comments