<?php
// Make sure the PEAR directory is in our include path. 
// This is the directory where I put it on my laptop. 
// To install PEAR on your *NIX system, please see 
// curl http://pear.php.net/go-pear | php -q
ini_set('include_path', '/Users/alex/puredata/projets/phpext' . ':' . ini_get('include_path'));
// include the classes
require_once 'Net/DataFlow/PhpExt.php';
/**
 * This is just a simple PD messages object written in PHP
 * 
 * You need to extend the Net_DataFlow_PhpExt PHP class in order 
 * to create a server to PD messages. 
 */
class PureData_Example extends Net_DataFlow_PhpExt {
    /**
     * @var array Array of values that can be [phpset( and [phpget( from PD
     * This is accessible through our on_phpset and on_phpget methods via 
     * the PD messages [phpset <key> <value>( and [phpget <key> <value>(
     */
    var $_vars = array();
    /**
     * Sets the value of a variable
     */
    function on_phpset() {
        $args = func_get_args();
        $key = $args[0];
        unset($args[0]);
        if (is_string($key) || is_int($key)) {
            $this->_vars[$key] = $args;
        }
    }
    /** 
     * gets the value of a variable
     */
    function on_phpget($key) {
        if (isset($this->_vars[$key])) {
            $this->send($this->_vars[$key]);
        }
    }
    /**
     * Asks to send this list to PD 
     */
    function on_tellme() {
        $args = func_get_args();
        $this->send($args);  
    }
    /**
     * Sends the result of the square root of the float argument to PD 
     */
    function on_sqrt($num) {
        if (is_numeric($num)) {
            $result = sqrt((float) $num);
            echo $result . " is the result \n";
            $this->send($result);
        }
    }
}
// Now, let's create the object, set the options and start it. 
$phpext =& new PureData_Example;
$phpext->setVerbose(TRUE); 
$phpext->setPdReceive('localhost', 9090, 'sequential'); // PHP listens this port
$phpext->setPdSend('localhost', 9092); // PHP sends to this port
$phpext->start();
// You're done !
?>