douyiyi5284 2012-05-01 12:09
浏览 15
已采纳

too long

I am quite new to programming, when I develop my program I use a simple strategy to debug it: I write the program to print along the debugging messages as it operate the significant statements, for example

function foo1($number)
{
  //foo_print("I am function foo1({$number}). <br/>");
  //foo_print("I am going to increase 'count' by {$number}. <br/>");

  $GLOBALS["count"] = $GLOBALS["count'] + $number;

  //foo_print("Now 'count' = {$GLOBALS["count"]}, I finished my task, BYE BYE. <br/>");

}

function isFoo($number)
{
  //foo_print("I am function isFoo({$number}). <br/>");
  //foo_print("I am checking if the number < 3 or not, if so, it is Foo, if not, it is not Foo. <br/>");
  if($number <= 3)
  {
    //foo_print("Found that number = {$number} <= 3, I return true, BYE BYE. <br/>");
    return true;
  }

  //foo_print("Found that number = {$number} > 3, I return false, BYE BYE. <br/>");
  return false;
}

I call them debugging messages but, as you see, they're actually the thoroughly comments describing what does the program do on each line. I just write the function foo_print() to print them out when I am debugging the program. And comment them out in real use.

Instead of inserting and removing the comment sign '//' line by line in and out when switch between real run mode and debugging mode, I have the function foo_print to do the work: It can be set to turn on or off.

define(FOO_PRINT, 1)
function foo_print($message)
{
  if(FOO_PRINT) print $message;
  // if FOO_PRINT == 0 then do nothing.
}

But I think this method is ineffective, it has to check FOO_PRINT every time before printing a message.

My question is either or both of the following

  • Can I do something to tell php to ignore my foo_print() function when I don't want to use it?

  • Perhaps, instead of using foo_print function, I should write the messages in plain comment style using '//' sign and then tell php interpreter to print those comment messages when in debugging mode. Can I do that?

I think, other than debugging ease, this method will be of advantage that it can help me understand the program when I come back to see it in later days. (It very long and complicated for me that I believe I will forget it soon.)

I found it very complicated for me now to use advanced IDEs and debugging tools to develop my program. I believe some of these advanced debugging tools can do something similar to what I want, but I've tried on PHP-eclipse and xdebug for a week and it got me nowhere. thank you very much.

  • 写回答

2条回答 默认 最新

  • douzao1119 2012-05-01 12:27
    关注

    You could define two functions, one of which outputs the debug data and the other one doesn't. Then use a variable name to contain the name of the function you want to call and do your debugging by calling the function in the variable. Like this:

    function debug_print($data) {
        echo $data;
    }
    function debug_none($data) {
    }
    
    $debug = 'debug_print';
    $debug('Testing one'); // This prints out 'Testing one'
    
    $debug = 'debug_none';
    $debug('Testing two'); // This doesn't print out anything
    

    If you do this, don't forget to add global $debug to any functions that want to use the function.

    EDIT: There is also a more object oriented way to achieve the same result. You could define an interface and write a couple of implementations for it, allowing you to choose which one to use at runtime.

    $debugmode = true;
    
    interface Debugger {
        public function out($data);
    }
    
    class EchoDebugger implements Debugger {
        public function out($data) {
            echo $data;
        }
    }
    
    class NullDebugger implements Debugger {
        public function out($data) {
            // Do nothing
        }
    }
    
    if($debugmode)
        $debugger = new EchoDebugger();
    else
        $debugger = new NullDebugger();
    
    $debugger->out('This will be output if $debugmode is true');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿