SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 27 of 27
  1. #26
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Marcus,

    I agree with 95% of what you said. My example code was just that: an example. In addition, the code I originally posted is being rewritten to make better use of abstract interfaces, a better coupling system, parameter validation, and so on. As for the site being down...well, crud happens. I do in fact have a particular project in mind in regards to this implementation, but I can't go into much detail about that at this time.

    Drew

    And I did appreciate the comments after all. Seemingly contrary statements are still valid as long as they are presented in a civilized manner.

    [original code]
    PHP Code:
    <?php
    class EventBroker {
    // class vars
    private $_events;
    public function 
    __construct($events) {
    // get the list of supported events
    $events explode(','$events);
    // add each to the array
    foreach ($events as $event) {
    $this->_events[$event] = array();
    // end foreach
    // end function __construct
    public function Register(&$listener$method$event) {
    // register this callback with the event
    if (array_key_exists($event$this->_events)) {
    $this->_events[$event][] = array(
        
    'listener' => $listener,
        
    'method' => $method
    );
    // end if
    // end function Register
    public function RaiseEvent($event$args = array()) {
    // get the handlers for the event
    $handlers $this->_events[$event];
    // call each handler for the event
    foreach ($handlers as $handler) {
    call_user_func_array(array($handler['listener'], $handler['method']), $args);
    // end foreach
    // end function RaiseEvent
    // end class EventBroker
    class Pet extends EventBroker {
    public function 
    __construct() {
    // initialize supported events
    parent::__construct('Hiss,Purr');
    // end function __construct
    public function Kick() {
    // raise the Hiss event
    parent::RaiseEvent('Hiss');
    // end function Kick
    public function Stroke() {
    // raise the Purr event
    parent::RaiseEvent('Purr');
    // end function Stroke
    // end class Pet
    class Owner extends EventBroker {
    // class vars
    private $_pet;
    public function 
    __construct() {
    // create the pet
    $this->_pet = new Pet();
    // register a listener for two events (in vb this is like withevents)
    $this->_pet->Register($this'HandleHiss''Hiss');
    $this->_pet->Register($this'HandlePurr''Purr');
    // end function __construct
    public function __destruct() {
    // kill the pet :)
    unset($this->_pet);
    // end function __destruct
    public function HandleHiss() {
    // get any args passed
    $args func_get_args();
    // just spit out a notice
    echo ' the pet hissed. ';
    // end function HandleHiss
    public function HandlePurr() {
    // get any args passed
    $args func_get_args();
    // just spit out a notice
    echo ' the pet purred. ';
    // end function HandlePurr
    public function KickThePet() {
    // make owner kick the pet
    echo ' I kicked my pet and ';
    $this->_pet->Kick();
    // end function KickThePet
    public function StrokeThePet() {
    // make owner stroke the pet
    echo ' I stroked my pet and ';
    $this->_pet->Stroke();
    // end function StrokeThePet
    // end class Owner
    // BEGIN SAMPLE CODE
    $me = new Owner();
    $me->KickThePet();
    $me->StrokeThePet();
    unset(
    $me);
    // END SAMPLE CODE
    ?>

  2. #27
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Last week I discovered something, to me anyways but related to this discussion called SEDA, stands for Staged Event Driven Architecture

    Has anyone else come across this, and if so what'd they make of it? To me, seams to be what this whole discussion is about...

    Also, is event driven programming the way forwards? Big business seams to like the idea is the impression I get.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •