从类方法中删除回显-PHP

从类方法中删除回显-PHP,php,Php,我正在尝试从类外部移除类方法的回显。我使用以下代码成功地做到了这一点: function rewrite() { $rewrite = new ReflectionMethod( 'WPLogin', 'head' ); $filename = $rewrite->getFileName(); $start_line = $rewrite->getStartLine(); $end_line = $rewrite->getEndLine()-1

我正在尝试从类外部移除类方法的回显。我使用以下代码成功地做到了这一点:

function rewrite() {
    $rewrite = new ReflectionMethod( 'WPLogin', 'head' );

    $filename = $rewrite->getFileName();
    $start_line = $rewrite->getStartLine();
    $end_line = $rewrite->getEndLine()-1;

    $length = $end_line - $start_line;
    $source = file($filename);
    $body = implode('', array_slice($source, $start_line, $length));
    $body = preg_replace( '/echo \'\<\!(.*?)\n/', '', $body);

    eval($body);
}
函数重写(){
$rewrite=newreflectionmethod('WPLogin','head');
$filename=$rewrite->getFileName();
$start_line=$rewrite->getstartine();
$end_line=$rewrite->getEndLine()-1;
$length=$end\u line-$start\u line;
$source=文件($filename);
$body=内爆(“”,数组_切片($source,$start_line,$length));

$body=preg\u replace('/echo\'\只需将方法调用包装为:


这将删除该函数所做的任何输出,例如,
echo
print\u r
var\u dump

我对如何实现这一点有点困惑,因为我没有调用该方法,该方法基本上已经在核心本身中启动。@J.Doe您能详细说明如何调用该方法/类以及核心是什么吗我不明白你的意思:“这个方法基本上已经在核心中启动了”。我在WordPress中使用这段代码,类方法来自另一个插件。在这个方法中有一些广告我想删除。所以我必须在方法已经加载时从类外删除它。@J.Doe这有点诡计:)如果你的代码没有调用该方法,那么控制输出就会困难得多。在你的情况下,我认为你的原始代码是我能想到的最好的解决方案。谢谢你的建议。有没有其他方法使用此代码来避免
eval
?经历了所有这些故事后,我有点害怕使用它。
ob_start();

// Call method here.

ob_end_clean();