聘我网

新概念招聘3.0

PHP中有没有办法打印出函数的具体定义?

vote up0vote downstar

JS中可以用alert直接打印出来,php呢?

 

1 个答复

vote up0vote downcheck

可以用getFileName,getStartLine,getEndLine自己实现一个:

printFunction(array('Foo','bar'));
printFunction('bar');


class Foo {
  public function bar() {
    echo '...';
  }
}

function bar($x, $y, $z) {
  //
  //
  //
  echo 'hallo';

  //
  //
  //
}
//


function printFunction($func) {
  if ( is_array($func) ) {
    $rf = is_object($func[0]) ? new ReflectionObject($func[0]) : new ReflectionClass($func[0]);
    $rf = $rf->getMethod($func[1]);
  }
  else {
    $rf = new ReflectionFunction($func);
  }
  printf("%s %d-%d\n", $rf->getFileName(), $rf->getStartLine(), $rf->getEndLine());
  $c = file($rf->getFileName());
  for ($i=$rf->getStartLine(); $i<=$rf->getEndLine(); $i++) {
    printf('%04d %s', $i, $c[$i-1]);
  }
}
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.