简体   繁体   English

如何使用其他类的PHP魔术常数?

[英]how to use PHP magic constants from other classes?

i have created below function in a file info.php to debug variable & data during page load 我在文件info.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) 
       {
            echo "<br/>METHOD:".__METHOD__;
            echo "<br/>LINE:".__LINE__;
       }
    }
 }

now i call this method from another page index.php , where i inculded info.php 现在我从另一个页面index.php调用此方法,在该页面中我灌入了info.php

in this file i want to debug POST array, so i write below code 在此文件中,我想调试POST数组,所以我在下面的代码中编写

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 一切正常,但方法和LINE出现在下方

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. 因此,如果我将$more=TRUE放在watch() ,该怎么做,那么方法和行号应该与调用它的类截然不同。

can i use self:: or parent:: in watch method?? 我可以在手表方法中使用self::parent::吗? or something else please suggest me how to call magic constants from other classes or is there any other method to debug varaibale?? 或其他什么建议我如何从其他类中调用魔术常数,或者是否有其他方法可以调试varaibale? ( please dont suggest to use Xdebug or any other tools) (请不要建议使用Xdebug或任何其他工具)

You have to use the debug_backtrace() php function. 您必须使用debug_backtrace() php函数。

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'];
                }

           }
        }
     }

You can not use those constants from that scope. 您不能在该范围内使用这些常量。 Check out the function debug_backtrace() instead. 请改用功能debug_backtrace() If it gives you too much info, try to parse it. 如果它给您太多信息,请尝试解析它。

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. debug_bactrace是可以完全自动执行此操作的唯一方法,但这是一个“重载”功能。...执行起来很慢,需要解析来提取所需的信息。 It might seem cumbersome, but a far better solution is to pass the necessary information to your Info::watch method: 看起来很麻烦,但是更好的解决方案是将必要的信息传递给您的Info :: watch方法:

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); 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM