来自SWIG生成的PHP扩展的stdout

时间:2010-07-19 17:52:29

标签: php stdout swig php-extension

我有以下C ++函数:

void foo() {
    std::cout << "bar" << std::endl;
}

我通过SWIG将其移植到PHP。一切都编译好,扩展加载正确。我可以从PHP调用foo(),但是如果我从命令行运行PHP脚本,我只看到bar输出。

$ php script.php
bar

如果我在浏览器中加载脚本,则不会显示任何内容。为什么在这种情况下不显示bar

1 个答案:

答案 0 :(得分:1)

您无法直接打印到stdout。这当然只有在您使用CLI SAPI时才有效。使用php_printf或其中任何一个:

//Calls php_output_write
#define PHPWRITE(str, str_len)
//Calls php_output_write_unbuffered
#define PHPWRITE_H(str, str_len)
//Idem:
#define PUTC(c)
#define PUTC_H(c)
#define PUTS(str)
#define PUTS_H(str)
int php_write(void *buf, uint size TSRMLS_DC);
int php_printf(const char *format, ...);
int php_output_write(const char *str, size_t len TSRMLS_DC);
int php_output_write_unbuffered(const char *str, size_t len TSRMLS_DC);
//see the rest of main/output.c