Home Backend Development PHP Tutorial 私家调试函数求优化

私家调试函数求优化

Jun 13, 2016 pm 12:57 PM
debug echo gt lt nbsp

私人调试函数求优化
1.可能有点异想天开,我希望获取调用这个函数的文件的文件名和行数,目前的死办法是外面传__FILE__和__LINE__

2.我想获取外面传的调试变量的名称,例如你有个变量叫  $abc = 'fdsafs'; 这样赋值了,然后  debug($abc);   我能在这个函数内部想办法知道这个变量的名称叫  abc

3.获取变量类型,类似别的语言的typeof,现在来一个变量,假设它是字符串,却没办法echo typeof($data)   ,却要if(is_string($data))echo 'string';...

4.你能提出的更多优化建议,写一写你的改造结果~


====================
这个函数从当初最简单的
function debug($data, $die = false){
    echo '

';<br>
    print_r($data);<br>
    echo '
Copy after login
';
    if($die)die();
}
到今天如下面的代码,已经是一年多了~方便自己调试变量值.我也懒得学那些什么复杂的调试框架什么xDebug..其实我要调试的需求在上面说的和下面代码体现的都有了,没更多的要求.
大家有没有试过,在一个很大的程序体里,到处echo一些值,在调试完后修正代码后,运行时还发现有调试的echo值输出,却不记得这个echo放哪里了- -   所以我需要知道这个调用的文件位置和行号.包括其它更多的调试需求,其实也不是很多,知道变量内容,长度,类型,它变成这个内容的文件位置就行了.



上代码:

$a = 'xx';
$b = 45;
$c = true;
$d = false;
$e = null;
$f = new stdClass();
$g = array(
    'a' => 'v1',
    'b' => 'v2',
);


debug($a);
debug($b);
debug($c);
debug($d);
debug($d);
debug($e);
debug($f);
debug($g);


function debug($data, $die = false, $file = null, $line = null){
    static $debugCount = 0; //调试计数器
    echo '
================= 新的调试点: '.++$debugCount.'========================
';
    if($file)echo "文件名: $file
\n";
    if($line)echo "行数: $line
\n";
    $len = strlen($data);
    if($len)echo '内容长度:'.$len."
\n";
    echo "变量名称: a
\n"; //有没有办法获取到外面传参时,debug($a);的变量名称 'a'?
    echo "
\n";<br>
    print_r(var_export($data, true));<br>
    echo "
Copy after login
\n";


    if($die){
        exit('
返回');
    }
    else return $debugCount;
}

------解决方案--------------------
function debug()<br />
{<br />
    global $config;<br />
    if(!$config['debug']) {<br />
        return;<br />
    }<br />
<br />
    echo '<pre class="brush:php;toolbar:false">';<br />
    foreach(func_get_args() as $var)<br />
    var_dump($var);<br />
    echo '
Copy after login
';
}
function fdebug()
{
    global $config;
    if(!$config['debug']) {
        return;
    }
    foreach(func_get_args() as $var) {
        file_put_contents('./aaa_yii_debug.php', ''.PHP_EOL . PHP_EOL, FILE_APPEND);
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

How to enable or disable taskbar thumbnail previews on Windows 11

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

OOBELANGUAGE Error Problems in Windows 11/10 Repair

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

Display scaling guide on Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

See all articles