function func() {
// ...
}
Ben işlev adını "func"
, ama onun bir tanımı var.
JavaScript, ben sadece alert()
tanımını görmek için kullanmak istiyorum.
PHP benzer bir işlevi var mı?
Sen yöntemler getFilename (), getStartLine (), getEndLine () (eğer varsa) ReflectionFunctionAbstract kaynak dosyadan işlevleri / yöntemleri kaynak kodunu okumak için tanımlanan kullanabilirsiniz.
örneğin (Hata işleme olmazsa)
<?php
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]);
}
}
I don't know of one. altındaki kod bakın. A function to list all the defined functions bulunmaktadır. Orada başka bir get the values of all the arguments to the current function için, ve number of arguments. Ve see if a function exists için bir tane var. Ancak mevcut fonksiyonunu, ne de biçimsel parametreleri listeleyen herhangi bir araç isim biri olarak görünmüyor.
Bir çalışma zamanı hatası oluştuğunda bile, bir çağrı yığını listesi, ne de aktif durumda işlevini devlet değil:
PHP Warning: Division by zero in t.php on line 6
Edit: nerede olduğunu belirlemek için bir kod, bu ekleyin:
echo "At line " .__LINE__ ." of file " . __FILE__ ."\n";
Bu çıktılarının verir
At line 7 of file /home/wally/t.php
2 Edit: Ben ne istediğiniz gibi gözüküyor ki benim kod bu işlevi bulundu:
function traceback ($showvars)
{
$s = "";
foreach (debug_backtrace($showvars) as $row)
{
$s .= "$row[file]#$row[line]: ";
if(isset($row['class']))
$s .= "$row[class]$row[type]$row[function]";
else $s .= "$row[function]";
if (isset($row['args']))
$s .= "('" . join("', '",$row['args']) . "')";
$s .= "<br>\n";
}
return $s;
}
Örneğin üretir:
[wally@zf ~]$ php -f t.php
/home/wally/t.php#24: traceback('1')<br>
/home/wally/t.php#29: t('1', '2', '3')<br>
/home/wally/t.php#30: x('2', '1')<br>
/home/wally/t.php#31: y('2', '1')<br>
/home/wally/t.php#33: z('1', '2')<br>