php-防止对其他函数的调用更改标题

php-防止对其他函数的调用更改标题,php,header,echo,content-type,Php,Header,Echo,Content Type,我有以下代码: $result = array('output'=>'success'); header("Content-Type: application/json; Charset=UTF-8"); echo json_encode($result); 但是,当我按如下方式调用另一个函数时,标题将更改为text/html $myfunc->call(); $result = array('output'=>'success'); header("Content-Type

我有以下代码:

$result = array('output'=>'success');
header("Content-Type: application/json; Charset=UTF-8");
echo json_encode($result);
但是,当我按如下方式调用另一个函数时,标题将更改为
text/html

$myfunc->call();
$result = array('output'=>'success');
header("Content-Type: application/json; Charset=UTF-8");
echo json_encode($result);

基本上
$myfunc->call()更改标题并回显文本。这导致我的代码失败。如何忽略
$myfunc->call()中标题和输出的更改

我能想到的唯一一件事是向该函数传递一个参数,指示是否应执行头代码,然后将该段代码包装在一个条件语句中。但是,只有在您可以自由更改函数的情况下,才可以这样做。否则,您将被卡住,因为无法设置两次标题

function call($setHeaders=true) {
    ...
    if ($setHeaders) {
        // Code for seting the header
    }
}

“忽略”函数/方法的输出的唯一方法是不要调用它,或者使用
return
关键字,而不是
echo
print
>因为无法设置两次标题,所以被卡住了。好啊