PHP

OOP Intricacies

Static properties are inherited, but in order to use them “intuitively”, you need to use the, erm, static keyword. E.g.:

class sync_message {

    var $message;
    var $datetime;
    public static $filename = 'synchronizer.log';

    function write_log() {
        // simplified example
        $handle = fopen(static::$filename);
    }
}

class sync_warning {
   public static $filename = 'warnings.log';
}

So, in order to have sync_warning use the file name warnings.log, you need to use static::$filename. And not, as you might expect, the keyword self. The self keyword does a lookup in the class where the method which uses the static variable was originally defined.

Profiling a PHP Application with Xdebug

Install xdebug and kcachegrind through apt-get or something similar.

Now put this in your php.ini (usually '/etc/php5/apache2/php.ini'):

;xdebug.profiler_enable=On
;; Make sure that xdebug has read/write access in the following dirs:
xdebug.profiler_output_dir="/home/onno/php/xdebug"
xdebug.trace_output_dir="/home/onno/php/xdebug"
; trigger is GET or POST variable XDEBUG_PROFILE
xdebug.profiler_enable_trigger=On
; %s = full path, %p = process id, %H = $_SERVER['HTTP_HOST']
;xdebug.profiler_output_name ="cachegrind.out.%H.%p"
xdebug.profiler_output_name ="cachegrind.out.%s.%p"

View the output of xdebug (written to /home/onno/php/xdebug in this example) in kcachegrind.

MYSQL BUG

While using MySql version 5.0.7 or higher you will encounter a bug in moodle 2.0 or lower. This error occures in the Assignment module.

BIGINT UNSIGNED value is out of range in '(`spring`.`s`.`timemarked` - `spring`.`s`.`timemodified`)'

To fix this you have to replace the following line in the assignment/lib.php file:

COALESCE(SIGN(SIGN(s.timemarked) + SIGN(s.timemarked - s.timemodified)), 0) AS status ';

To:

COALESCE(SIGN(CAST(s.timemarked as SIGNED) + SIGN(CAST(s.timemarked as SIGNED) - CAST(s.timemodified as SIGNED))), 0) AS status ';

Use a search and replace tool, the line that has to be replaced is in this document twice, by default.

phpdocumentor

Tool to create API documentation, based on inline comments. Type phpdoc -h to get help. Visit this Quickstart to get more info (but be warned: the documentation for phpdoc is very wordy).

Install

This worked for me under Ubuntu (10.04 LTS - the Lucid Lynx):

sudo pear install PhpDocumentor

Usage

phpdoc -t docs -d . -s on -dn Soda
  • -t: target directory where docs are placed
  • -d: directory to look for source code
  • -s: include source code (default: off)
  • -dn: defaultpackagename (here: Soda - this only works if each of your source files contains a directive @package Soda at the top)

Please note that you style the first page of your API documentation with a 'tutorial'. phpDocumentor needs the tutorials in a very specific place: soda/tutorials/Soda/Soda.pkg, where soda is the same dir as the one mentioned under -d.

Here's a sample tutorial:

<refentry id="{@id}">
 <refnamediv>
  <refname>Soda API Documentation</refname>
 </refnamediv>
 <refsect1>
  <title>Soda</title>
  <para>
    Soda is a Moodle module to develop new modules. These web pages contain the API documentation for Soda's classes.
  </para>
  <para>
    A basic tutorial for Soda is located here: {@link http://tech.solin.eu/doku.php?id=moodle:using_soda_to_create_new_moodle_modules tech.solin.eu} (use right-click to open in new tab or window).
  </para>
  <para>
    Copyright (C) 2011 Solin - www.solin.eu
  </para>
  <para>
    Programming and development:
    Onno Schuit (o.schuit.at-or-around.solin.nl)
  </para>
 </refsect1>
</refentry>

Personal Tools