PHP Classes

File: evt_test

Recommend this page to a friend!
  Classes of av   AVlib Event   evt_test   Download  
File: evt_test
Role: Example script
Content type: text/plain
Description: Example event test
Class: AVlib Event
Trigger and dispatch custom script events
Author: By
Last change:
Date: 14 years ago
Size: 2,059 bytes
 

Contents

Class file image Download
<?php

require_once 'AVlib/Event/Dispatcher.php';

class
example_event extends AVlib_Event
{

    const
EXAMPLE = 'example_example';

    public
$example_var;

    public function
__construct ( $type, $bubbles = false, $cancelable = false, $example_var ) {
       
       
//construct the parent
       
parent::__construct ( $type, $bubbles, $cancelable );
       
       
//store Your own custom variables
       
$this->example_var = $example_var;
   
    }

    public function
cloneEvent ( $eventPhase, $target, $currentTarget ) {
       
$evt = parent::cloneEvent ( $eventPhase, $target, $currentTarget );
       
       
/*
         * do something
         * $this->example_var was copied to clone by parent::cloneEvent
         *
         * so you should extend this function only if you got some other cloning to do
         */
       
       
return $evt;
    }
   
    public function
toString() {
        return
parent::toString(array('example_var' => $this->example_var));
    }
}

class
disp1 extends AVlib_Event_Dispatcher
{

    function
__construct ( ) {
       
       
//we set up a event listener for event 'loaded' and handle them in listener function
       
$this->addEventListener ( example_event::EXAMPLE, array (
               
$this,
               
'listener'
       
) );
       
       
//now we make 10 disp2 objects
       
for ( $i = 0; $i < 10; $i++ ) {
            new
disp2 ( );
        }
    }

    public function
listener ( AVlib_Event $event ) {
       
        echo
$event;
       
   
/*
         * if that event is stoppable, i could stop it on this branch or stop
         * it immediately, so it will not bubble further up to (main)
         */
   
   
}
}

class
disp2 extends AVlib_Event_Dispatcher
{

    public function
__construct ( ) {
       
$evt = new example_event ( example_event::EXAMPLE, true, false, 'will be catched every time' );
       
$this->dispatchEvent ( $evt );
    }
}

$d = new disp1 ( );