Actions

Debugging: Difference between revisions

From LimeSurvey Manual

No edit summary
 
No edit summary
Line 12: Line 12:
Yii<center>trace(CVarDumper</center>dumpAsString($variable), 'vardump');</syntaxhighlight>
Yii<center>trace(CVarDumper</center>dumpAsString($variable), 'vardump');</syntaxhighlight>


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.
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>
 
As a bonus you will also get the file and line that called the traceVar function to assist in debugging.

Revision as of 11:03, 13 November 2012

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<center>trace(CVarDumper</center>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.