tags:

views:

46

answers:

3

Hello All,
i have created below function in a file info.php to debug variable & data during page load

 class Info {
    static function watch($what,$msg='',$more=FALSE)
    {
        echo "<hr/>";
        echo "<br/>".$msg.":";
        if( is_array($what) || is_object($what) )
        {
            echo "<pre>";
            print_r($what);
            echo "</pre>";
        }
        else{
                echo $what;
        }
       if($more) 
       {
            echo "<br/>METHOD:".__METHOD__;
            echo "<br/>LINE:".__LINE__;
       }
    }
 }

now i call this method from another page index.php, where i inculded info.php

in this file i want to debug POST array, so i write below code

class Testpost {
    __construct() { // some basic intializtion }
    function getPostdata($postarray) {
              $postarray=$_POST;
    Info::watch($postarray,'POST ARRAY', TRUE);
}

everything is working fine but method and LINE appears below

METHOD:Info::watch();
LINE:17 // ( where this code is written in Info class)

but i wantbelow to display

METHOD: Testpost::gtPostdata()
LINE:5( where this function is called in Testpost class)

so how do i do that if i put$more=TRUE in watch() then method and line number should be diaply from the class where it is called.

can i use self:: or parent::in watch method?? or something else please suggest me how to call magic constants from other classes or is there any other method to debug varaibale?? ( please dont suggest to use Xdebug or any other tools)

+1  A: 

You can not use those constants from that scope. Check out the function debug_backtrace() instead. If it gives you too much info, try to parse it.

chelmertz
+1  A: 

You have to use the debug_backtrace() php function.

You also have below the solution to your problem. Enjoy! :)

 <?php
    class Info {
        static function watch($what,$msg='',$more=FALSE)
        {
            echo "<hr/>";
            echo "<br/>".$msg.":";
            if( is_array($what) || is_object($what) )
            {
                echo "<pre>";
                print_r($what);
                echo "</pre>";
            }
            else{
                    echo $what;
            }
           if($more) 
           {

                $backtrace = debug_backtrace();
                if (isset($backtrace[1])) 
                {
                    echo "<br/>METHOD:".$backtrace[1]['function'];
                    echo "<br/>LINE:".$backtrace[1]['line'];
                }

           }
        }
     }
Bogdan Constantinescu
@Bogdan Thanks a lot for solution, it works fine, but i have still two problem 1) it display wrong line number 2) is there no function similar to `__METHOD__` (class name:: method name) , i have to write `$backtrace[1]['class'] ".::` `."$backtrace[1]['function']`
JustLearn
@bogdan why u have used `$backtrace[1]`, can't we use `$backtrace[0]`???i have figured that if i use `$backtrace[0]['line']` then it gives correct line number
JustLearn
`$backtrace[0]` it's "yourself", as in the class itself. `$backtrace[1]` shows the line where the method that calls info was called
Bogdan Constantinescu
+1  A: 

debug_bactrace is the only way you could totally automate this, but it's a "heavy-duty" function.... very slow to execute, and needs parsing to extract the required information. It might seem cumbersome, but a far better solution is to pass the necessary information to your Info::watch method:

class Info { 
    static function watch($whereClass,$whereLine,$what,$msg='',$more=FALSE) 
    { 
        echo "<hr/>"; 
        echo "<br/>".$msg.":"; 
        if( is_array($what) || is_object($what) ) 
        { 
            echo "<pre>"; 
            print_r($what); 
            echo "</pre>"; 
        } 
        else{ 
                echo $what; 
        } 
       if($more)  
       { 
            echo "<br/>METHOD:".$whereClass; 
            echo "<br/>LINE:".$whereLine; 
       } 
    } 
 } 
now i call this method from another page index.php, where i inculded info.php

class Testpost { 
    __construct() { // some basic intializtion } 
    function getPostdata($postarray) { 
              $postarray=$_POST; 
    Info::watch(__METHOD__,__LINE__,$postarray,'POST ARRAY', TRUE); 
} 
Mark Baker
that i was doing already in my code, but i have to write `__METHOD__` AND `__LINE__` each time, that's why i had asked question..anyway thanks for reply
JustLearn