Optimization of private debugging functions

Source: Internet
Author: User
Optimization of private debugging functions 1. it may be a bit whimsical. I want to obtain the FILE name and number of lines that call this function. the current dead-end method is to pass _ FILE _ and _ LINE__2 outside. I want to get the name of the debug variable passed outside. for example, you have a variable named & nbsp; $ abc & nbsp ;=& nbsp; 'F. optimize the private debug function.
1. it may be a bit whimsical. I want to obtain the FILE name and number of lines that call this function. the current dead-end method is to pass _ FILE _ and _ LINE __

2. I want to get the name of the debug variable passed outside. for example, if you have a variable named $ abc = 'fdsafs', assign a value and debug ($ abc ); in this function, I can find a way to know the variable name is abc.

3. get the type of the variable, similar to the typeof of other languages. now we have a variable, assuming it is a string, but there is no way to echo typeof ($ data), but if (is_string ($ data )) echo 'string ';...

4. for more optimization suggestions, write down your transformation results ~


================================
This function is the simplest
Function debug ($ data, $ die = false ){
Echo'
';
    print_r($data);
    echo '
';
If ($ die) die ();
}
The following code has been used for more than a year ~ Debug variable values by yourself. I am too lazy to learn what complicated debugging frameworks and xDebug .. in fact, I need to debug all the above and below code, and there are no more requirements.
Have you ever tried to echo some values everywhere in a large program Body? after the code is corrected after debugging, debugging echo value output is also found during running, but I don't remember where the echo is.-so I need to know the location and row number of the called file. there are not many other debugging requirements. you only need to know the variable content, length, type, and file location.



Code:

$ A = 'XX ';
$ B = 45;
$ C = true;
$ D = false;
$ E = null;
$ F = new stdClass ();
$ G = array (
'A' => 'v1 ',
'B' => 'V2 ',
);


Debug ($ );
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; // debug the counter
Echo' ==================== New debugging point: '. ++ $ DebugCount .'====================================== ';
If ($ file) echo "file name: $ file \ N ";
If ($ line) echo "number of rows: $ line \ N ";
$ Len = strlen ($ data );
If ($ len) echo 'content length: '. $ len ." \ N ";
Echo "variable name: \ N "; // Is there a way to get the variable name 'A' of debug ($ a); when passing parameters outside '?
Echo"
\n";
    print_r(var_export($data, true));
    echo "
\ N ";


If ($ die ){
Exit (' Return ');
}
Else return $ debugCount;
}
------ Solution --------------------
function debug()
{
    global $config;
    if(!$config['debug']) {
        return;
    }

    echo '
';
    foreach(func_get_args() as $var)
    var_dump($var);
    echo '
';
}
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.