Filters derived from PHP classes

A less well-known coding technique makes it possible to also execute a PHP class method. When requested to process a filter written as “x::y”, SPIP will look for a “filter_x” PHP class with an executable “y” method. If it doesn’t find one, it will then look for a class “filtre_x_dist”, and then finally for a class “x”.

  1. [(#TAG|class::method)]

Example

Let’s imagine a PHP class which has been defined as shown below. It contains a (recursive) function which calculates factorials (x! = x*(x-1)*(x-2)*...*3*2*1).

  1. class Math{
  2. function factorial($n){
  3. if ($n==0)
  4. return 1;
  5. else
  6. return $n * Math::factorial($n-1);
  7. }
  8. }

Download

This new function could be called within SPIP as follows:

  1. [(#VAL{9}|Math::factorial)]
  2. // returns 362880

Download

Author Mark Baber Published : Updated : 19/06/10

Translations : English, français