Zend扩展,得到echo的参数?

时间:2011-10-04 07:37:04

标签: php-internals

我们已经做了一个Zend扩展,我们想要写出zval的echo的地址应该写出来,但是我们无法弄清楚如何接收它们,因为我们注意到echo“test”之间存在差异;和$ a =“测试”; echo $ a;

.... Some stuff that overrides the echo opcode ....

FILE *tmpfile;
int echo_handler(ZEND_OPCODE_HANDLER_ARGS)
{
    zend_op *opline = execute_data->opline;
    tmpfile = fopen("/tmp/echo.test","a+");
    fprintf(tmpfile,"Echo was called\n");
    fclose(tmpfile);

    return ZEND_USER_OPCODE_DISPATCH;
}

我们如何获得参数,无论它是否是变量?

1 个答案:

答案 0 :(得分:0)

echo的处理程序是

static int ZEND_FASTCALL  ZEND_ECHO_SPEC_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
    zend_op *opline = EX(opline);

    zval z_copy;
    zval *z = &opline->op1.u.constant;

    if (IS_CONST != IS_CONST &&
        Z_TYPE_P(z) == IS_OBJECT && Z_OBJ_HT_P(z)->get_method != NULL &&
        zend_std_cast_object_tostring(z, &z_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
        zend_print_variable(&z_copy);
        zval_dtor(&z_copy);
    } else {
        zend_print_variable(z);
    }

    ZEND_VM_NEXT_OPCODE();
}

来自Zend/zend_vm_execute.h,正如您所看到的,它基本上就是调用zend_print_variable()

挂钩这个功能,你应该走在正确的轨道上。

Bonus:它也适用于print语句。