Difference between revisions of "Sven/PHP"

From Organic Design wiki
(See also: MW:Extensions_FAQ)
(Logging to file)
Line 32: Line 32:
 
*[http://www.php.net/get_parent_class get_parent_class]
 
*[http://www.php.net/get_parent_class get_parent_class]
  
From [[Sven/Notes]];
+
From [[Sven/Notes]], [[Extension:Fasta.php]];
 
<php>
 
<php>
 
# Logging to an article from within the MW environment
 
# Logging to an article from within the MW environment
Line 39: Line 39:
 
$la = new Article(Title::newFromText($article));
 
$la = new Article(Title::newFromText($article));
 
$la->quickEdit($la->getContent()."\n*$ts: $msg");
 
$la->quickEdit($la->getContent()."\n*$ts: $msg");
 +
}
 +
 +
# Logging to a file
 +
function logFile($file, $msg) {
 +
$fh = fopen($file,'a');
 +
$ts = $GLOBALS['wgLang']->timeanddate(wfTimestampNow(),true);
 +
if(is_array($msg)) $msg = print_r($msg, true);
 +
fwrite($fh, "\n$ts: $msg");
 
}
 
}
 
</php>
 
</php>
 +
 +
  
 
==PHP errors==
 
==PHP errors==

Revision as of 22:28, 9 December 2007

Debugging PHP

Info.svg 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
)


Info.svg See pages 147-153 (Programming php) for introspection functions for Objects in php.


From Sven/Notes, Extension:Fasta.php; <php>

  1. Logging to an article from within the MW environment

function logAdd($article,$msg) { $ts = $GLOBALS['wgLang']->timeanddate(wfTimestampNow(),true); $la = new Article(Title::newFromText($article)); $la->quickEdit($la->getContent()."\n*$ts: $msg"); }

  1. Logging to a file

function logFile($file, $msg) { $fh = fopen($file,'a'); $ts = $GLOBALS['wgLang']->timeanddate(wfTimestampNow(),true); if(is_array($msg)) $msg = print_r($msg, true); fwrite($fh, "\n$ts: $msg"); } </php>


PHP errors

To see PHP errors, add this to the very top of LocalSettings.php: <php> error_reporting(E_ALL); ini_set("display_errors", 1); </php>

See also