PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Robert Jäckel   EventDriven   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: extended class with testing
Class: EventDriven
Register and call event handlers
Author: By
Last change:
Date: 13 years ago
Size: 3,341 bytes
 

Contents

Class file image Download
<?php
// basic output function
function dump($o) {
    echo
nl2br(str_replace(' ','&nbsp;&nbsp;',print_r($o,1))).'<br/><br/>';
}

class
Store extends EventDriven {
   
// Speicher zum Überladen
   
protected $store = array();
   
// Events
   
protected $onBeforeSet;
    protected
$onAfterSet;
    protected
$onErrorSet;
   
// Event-Handling Events =)
   
protected $onBeforeSetHandler;
    protected
$onAfterSetHandler;
    protected
$onErrorSetHandler;
 
   
// Standard-Handler setzen
   
public function __construct() {
       
$this->on('beforeSet', array($this,'h'))->on('afterSet',array($this,'a'));
    }
   
// Event-gestütztes Overloading
   
public function __set($name, $value) {
        try {
           
// Prüfevent
           
$this->trigger('beforeSet',$this,$name,$value);
           
// Aktion
           
$this->store[$name]=$value;
           
// Ergebnis-Event
            // sollte zwar keine Exceptions werfen aber, sicher ist sicher
            // z.B. zum Loggen
           
$this->trigger('afterSet',$this,$name,$value);
        } catch (
Exception $e) {
           
// Fehler-Event
           
$this->trigger('errorSet',$this,$e,$name,$value);
        };
    }
   
// BeforeSet-Handler
   
protected function h ($me,$k,$v) {
       
dump("h Trying to set '$k' to '$v'");
    }
   
// AfterSet-Handler
   
protected function a () {
       
dump("a Property set.");
    }
   
// Überschriebene event-Setzung
    // z.B. um bestimmte events auszuschließen o.Ä.
    // grundfunktionalität sollte weiterhin bleiben
   
public function on($event, $cb, $overwrite = false) {
        try {
           
// Prüfevent
           
$this->trigger('beforeSetHandler',$this,$event,$cb,$overwrite);
           
// Aktion
           
parent::on($event, $cb, $overwrite);
           
// Ergebnis-Event
           
$this->trigger('afterSetHandler',$this,$event,$cb,$overwrite);
        } catch (
Exception $e) {
           
// Fehler-Event
           
$this->trigger('errorSetHandler', $this,$e,$event,$cb,$overwrite);
        }
        return
$this;
    }
}
// end of example class

// usage
$o = new Store();
dump('<h1>Event Test Suite</h1>');
dump('This suite shall be the first step in PHP\'s event handled programming.');
dump('<h2>Using this object:</h2>');
dump($o);
dump('<h2>Trying to set an undefined property...</h2>');
$o->test = 123;
dump('<h2>Result</h2>');
dump($o);
dump('<h2>Adding some Event-Handlers...</h2>');
 
$o->on('afterSetHandler',function(EventDriven $me,$event,$cb,$ow){
    static
$c = 0;
   
dump("0: $event ".($ow?"replaced.":"added").'. '.$c++);
})->
on('beforeSet',function(EventDriven $me,$k,$v){
   
dump ("1: Property '$k' not found. Trying to overload...");
})->
on('afterSet',function(EventDriven $me,$k,$v){
   
dump("2: Overloaded. You have set '$k' to '$v'");
},
1)->on('errorSet',function(EventDriven $me,Exception $e) {
   
dump($e->getMessage());
   
dump('3: Canceled.');
})->
on('beforeSet',function(EventDriven $me, $name){
    if(
$name == 'notAllowedProperty') throw new Exception ("4: Property <em>$name</em> not allowed.");
});
 
dump('<h2>What happens now?</h2>');
$o->toast = 456;
dump('');
$o->notAllowedProperty = 'At least I tried.';
dump('<h2>The altered object</h2>');
dump($o);