2011-09-05 94 views
1

我只是把它們放在一起來幫助調試一些PHP腳本。正如你所看到的那樣,它很sl but,但我會改進一些。獲取傳遞給PHP函數的變量名稱?

我的調試函數有2個變量傳入,一個變量名和一個變量值。

是否有可能只是傳遞變量,並以某種方式獲取變量的名稱,而無需手動執行,就像我現在設置的那樣?

的功能

<?php 
function debug($varname, $var) 
{ 
    echo '<br>' . $varname; 

    // $var is a STRING 
    if (is_string($var)) { 
     echo ' (string) = ' . $var . '<br>'; 

    // $var is an ARRAY 
    } elseif (is_array($var)) { 
     echo ' (array) = <pre>'; 
     print_r($var); 
     echo '</pre><br>'; 

    // $var is an INT 
    } elseif (is_int($var)) { 
     echo ' (int) = ' . $var . '<br>'; 

    // $var is an OBJECT 
    } elseif (is_object($var)) { 
     echo ' (object) = <pre>'; 
     var_dump($var); 
     echo '</pre><br>'; 
    } 
} 

測試

$testString = 'just a test!'; 
$testArray = array(
    'key1' => 'value1', 
    'key2' => 'value2', 
    'key3' => 'value3' 
    ); 
$testInt = 1234567890; 

$testObject = new stdClass; 
$testObject->someVar1 = 'testing123'; 
$testObject->someVar2 = '321gnitset'; 

debug('$testString', $testString); 
debug('$testArray', $testArray); 
debug('$testInt', $testInt); 
debug('$testObject', $testObject); 
?> 

結果...

$testString (string) = just a test! 

$testArray (array) = 
Array 
(
    [key1] => value1 
    [key2] => value2 
    [key3] => value3 
) 



$testInt (int) = 1234567890 

$testObject (object) = 
object(stdClass)#1 (2) { 
    ["someVar1"]=> 
    string(10) "testing123" 
    ["someVar2"]=> 
    string(10) "321gnitset" 
} 
+0

可能重複[如何在PHP中獲取變量名?(http://stackoverflow.com/questions/2384840/how-to-get-variable-name-in-php) – mario

+0

不正是你問,但你可以使用像Xdebug這樣的東西來實現這一點,甚至更多。 http://xdebug.org/docs/execution_trace('collect_params'配置與你的問題最相關的行爲。) – grossvogel

回答

2

如果你想知道這個名字,爲什麼不直接傳遞變量名的字符串常量,並使用全局數組訪問它(程序性計劃)

function foo($var) { 
    echo $var; //name of the variable 
    echo $GLOBALS[$$var]; //value of the variale 
} 

$bar = 'a string'; 
foo('bar'); 
+0

是的,全局是可能的 – ajreal

0

debug_print_backtrace()是你的朋友!將其添加到您的debug()功能。

如果您仍然只想調用函數名稱,您可以使用debug_backtrace()並在返回的數組中搜索前一個函數的名稱,在關聯數組結果中標識爲「函數」!

-2

SOLUTION:

$ ARGV - 傳遞給腳本的參數列表

示例:

<?php 
var_dump($argv); 
?> 

參考: PHP's $argv documentation

0

我將這些函數放在一起用於我自己的調試,只需將它們複製並保存在一個單獨的文件中,包含它並使用函數d()調用花哨的調試打印。它假設根據傳遞的變量類型對結果進行顏色編碼。

if(!defined('m_debug')) {define('m_debug', 1);} 
function d($var) { 
    if (m_debug) { 

     $bt = debug_backtrace(); 
     $src = file($bt[0]["file"]); 
     $line = $src[ $bt[0]['line'] - 1 ]; 

     //striping the inspect() from the sting 
     $strip = explode('d(', $line); 
     $matches = preg_match('#\(#', $strip[0]); 
     $strip = explode(')', $strip[1]); 
     for ($i=0;$i<count($matches-1);$i++) { 
      array_pop($strip); 
     } 
     $label = implode(')', $strip); 

      d_format($var, $label); 
    } 

} 

function l() { 
    global $super_dump_log; 

    if (func_num_args() > 0) { 
     $array = func_get_args(); 
     array_merge($super_dump_log, $array); 
    } else { 
     foreach($super_dump_log as $log){ 
      // 
     } 
    } 
} 

function d_format($var, $label) { 

    $colorVar = 'Blue'; 
    $type = get_type($var); 
    $colorType = get_type_color($type); 

    echo "<div class='m_inspect' style='background-color:#FFF; overflow:visible;'><pre><span style='color:$colorVar'>"; 
    echo $label; 
    echo "</span> = <span class='subDump' style='color:$colorType'>"; 
    if ($type == 'string') { 
     print_r(htmlspecialchars($var)); 
    } else { 
     print_r($var); 
    } 
    echo "</span></pre></div>"; 
} 

function get_type($var) { 

    if (is_bool($var)) { 
     $type = 'bool'; 
    } elseif (is_string($var)) { 
     $type = 'string'; 
    } elseif (is_array($var)) { 
     $type = 'array';   
    } elseif (is_object($var)) {  
     $type = 'object';  
    } elseif (is_numeric($var)) { 
     $type = 'numeric'; 
    } else { 
     $type = 'unknown'; 
    } 

    return $type; 
} 


function get_type_color($type) { 

    if ('bool' == $type) { 
     $colorType = 'Green'; 
    } elseif ('string' == $type) { 
     $colorType = 'DimGrey'; 
    } elseif ('array' == $type) { 
     $colorType = 'DarkOrchid'; 
    } elseif ('object' == $type) { 
     $colorType = 'BlueViolet'; 
    } elseif ('numeric' == $type) { 
     $colorType = 'Red'; 
    } else {  
     $colorType = 'Tomato'; 
    } 

    return $colorType; 
} 
相關問題