CPAINT :: Cross-Platform Asynchronous INterface Toolkit

Please note: CPAINT nor this website is under active development.

Upgrade: Backend Changes

Just like the frontend, the backend of CPAINT v2.x is completely object oriented. The first thing you will need to do is create a new instance of the cpaint object and tell it to start executing code with the cpaint.start() method.

Here is an example in v1.x and it's equivalent in v2.x that returns data to the frontend using XML.

v1.x

<?php
  require_once('cpaint.inc.php');

  function addNumbers($number1, $number2) {
    cpaint_xml_open_result('response');
    cpaint_xml_add_data('answer', 'answer', ($number1 + $number2));
    cpaint_xml_close_result();
    cpaint_xml_return_data();
  }
?>

v2.x

<?php
  require_once('cpaint2.inc.php');

  $cp = new cpaint();
  $cp->register('add');
  $cp->start();
  $cp->return_data();

  function add($num1, $num2) {
    global $cp;
    $cp->set_attribute('id', 'response');
    $cp->set_data($num1 + $num2);
  }
?>

As you can see, the new architecture makes it extremely easy to build XML responses, as it is naturally supported. You must remember to put global $[cpaint_object]; in every function or the call will fail.

If you aren't using XML, no problem. Here is a sample in both v1.x and the new method for v2.x:

v1.x

<?php
  require_once("cpaint.inc.php");

  function addNumbers($number1, $number2) {
    return ($number1 + $number2);
  }
?>

v2.x

<?php
  require_once("cpaint2.inc.php");

  $cp = new cpaint();
  $cp->register('add');
  $cp->start();
  $cp->return_data();

  function add($num1, $num2) {
    global $cp;
    $cp->set_data($num1 + $num2);
  }
?>

Notice - the only difference to the XML example is that I did not set an id attribute. However I could have set the id attribute, it wouldn't have made a difference.

You see, there is absolutely no reason for you to change the backend code only because the frontend (which you might have no control over) desired a different output format.