Notice: Undefined index: HTTP_ACCEPT in /var/local/cache/midgard/midgard/31-100-217-0.php(66) : eval()'d code on line 11

PHP5 events class

This entry was posted on 2008-08-04 12:42:01 EEST in 60° 43.992 N 24° 46.302 E Riihimäki, FI to

While I was building Duct Frameworks server side with PHP5 I realized I needed some kind of event support for it.
So I created similar approach that ActionScript uses. You can download the class with example from here.

Hopefully this comes in handy for someone else also. 

Here is simple example also:

 

<?php

class mother extends events_observable
{
public function __construct()
{
parent::__construct();

$this->addEventListener(events_event::ONCONSTRUCT, events::delegate($this, 'on_object_constructed'));
}

public function on_object_constructed($target, $args)
{
echo "{$target->name} has born.";
}
}

class child extends events_observable
{
public $name;

public function __construct($name)
{
parent::__construct();

$this->name = $name;

$this->dispatchEvent(new events_event(events_event::ONCONSTRUCT, $this));
}
}

$mother = new mother();
$child = new child('Jack');

?>
Back