Difference between revisions of "Sven/PHP"

From Organic Design wiki
Line 1: Line 1:
 
==Debugging PHP==
 
==Debugging PHP==
In MediaWiki there is a profiler that can be set, see [[MW:How to debug]].
+
{{Info:In MediaWiki there is a profiler that can be set, see [[MW:How to debug]].}}
  
Debuggers generally use a [[W:Stack trace]]. This can be approximated by using [http://www.php.net/print print] and [http://www.php.net/die die] statements in sections of code.
+
Debuggers generally use a [[W:Stack trace]]. This can be approximated by using [http://www.php.net/print print] and [http://www.php.net/die die] statements in sections of code. The combination of [http://www.php.net/print_r print_r] and [http://www.php.net/func_get_args func_get_args] allows you to return an array of args that a function was called with;
 
 
The combination of [http://www.php.net/print_r print_r] and [http://www.php.net/func_get_args func_get_args] allows you to return an array of args that a function was called with;
 
  
 
  [http://www.php.net/print_r print_r]([http://www.php.net/func_get_args func_get_args]());
 
  [http://www.php.net/print_r print_r]([http://www.php.net/func_get_args func_get_args]());
Line 12: Line 10:
 
  class Person {
 
  class Person {
 
   var $name = "Fred";
 
   var $name = "Fred";
   var $age = 35;
+
   var $age = 35;
 
  }
 
  }
 
  $o = new Person;
 
  $o = new Person;
Line 22: Line 20:
 
;Array
 
;Array
 
;(
 
;(
;  [name]] => Fred
+
;  [name] => Fred
;  [age] => 35
+
;  [age]   => 35
 
;)
 
;)
  

Revision as of 21:44, 8 December 2007

Debugging PHP

{{Info:In MediaWiki there is a profiler that can be set, see MW:How to debug.}}

Debuggers generally use a W:Stack trace. This can be approximated by using print and die statements in sections of code. The combination of print_r and func_get_args allows you to return an array of args that a function was called with;

print_r(func_get_args());

Casting an object of an array builds an array of the properties, mapping property names to values. e.g.

class Person {
 var $name = "Fred";
 var $age  = 35;
}
$o = new Person;
$a = (array) $o
print_r($a);

outputs;

Array
(
[name] => Fred
[age] => 35
)


PHP errors

To see PHP errors, add this to the very top of LocalSettings.php:

error_reporting(E_ALL);
ini_set("display_errors", 1);