Actions

Difference between revisions of "Debugging"

From LimeSurvey Manual

m
m (Text replacement - " enclose="div"" to "")
 
Line 8: Line 8:
 
To do a dump of a variable you can use the following syntax:
 
To do a dump of a variable you can use the following syntax:
  
<syntaxhighlight lang="php" enclose="div">$variable = $this->someMethod();  // What would this return?
+
<syntaxhighlight lang="php">$variable = $this->someMethod();  // What would this return?
  
 
Yii::trace(CVarDumper:dumpAsString($variable), 'vardump');</syntaxhighlight>
 
Yii::trace(CVarDumper:dumpAsString($variable), 'vardump');</syntaxhighlight>
Line 14: Line 14:
 
We make use of the <nowiki>Yii<center>trace()</nowiki> method, and pass it the result of the static call to <nowiki>CVarDumper</center>dumpAsString()</nowiki>. What it does is is similar to var_dump but it allows a little more control if you need if. The second parameter 'vardump' makes sure our messages gets logged. All this is a lot of typing and since programmers are lazy, we created a global function to save you some trouble. Instead of the lines above you can also use:
 
We make use of the <nowiki>Yii<center>trace()</nowiki> method, and pass it the result of the static call to <nowiki>CVarDumper</center>dumpAsString()</nowiki>. What it does is is similar to var_dump but it allows a little more control if you need if. The second parameter 'vardump' makes sure our messages gets logged. All this is a lot of typing and since programmers are lazy, we created a global function to save you some trouble. Instead of the lines above you can also use:
  
<syntaxhighlight lang="php" enclose="div">traceVar($variable);</syntaxhighlight>
+
<syntaxhighlight lang="php">traceVar($variable);</syntaxhighlight>
  
 
As a bonus you will also get the file and line that called the traceVar function to assist in debugging.
 
As a bonus you will also get the file and line that called the traceVar function to assist in debugging.

Latest revision as of 15:07, 16 February 2022

Debugging

Working with a framework can sometimes be hard to debug. In Yii we have some tools at our disposal to help debug the application. The very first thing to do is modify application/config.php and to set debug = 2 at the end. Now we get more errors presented and we can use a trace function to see what a variable's content is at any time during program execution.

If you want to see what queries are sent to the database, you can also enable debugsql by setting it to 1.

To do a dump of a variable you can use the following syntax:

$variable = $this->someMethod();  // What would this return?

Yii::trace(CVarDumper:dumpAsString($variable), 'vardump');

We make use of the Yii<center>trace() method, and pass it the result of the static call to CVarDumper</center>dumpAsString(). What it does is is similar to var_dump but it allows a little more control if you need if. The second parameter 'vardump' makes sure our messages gets logged. All this is a lot of typing and since programmers are lazy, we created a global function to save you some trouble. Instead of the lines above you can also use:

traceVar($variable);

As a bonus you will also get the file and line that called the traceVar function to assist in debugging.