PHP fireEvents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

PHP fireEvents

Post by Zoxive »

I'm working on my Framework, and thought it would be nice if i could implement Events like JavaScript has. So i don't have to Extend a Class just to make a few changes, or change a few settings or call some other functions, ex: have it insert into log on certain events.

Code: Select all

Class Events{

  static private $Events = array();

  public function fireEvent($name){
    if(isset(self::$Events[$name])){
      foreach(self::$Events[$name] as $method){
        $method(&$this);
        //call_user_func($method, &$this);
      }
      return true;
    }
    return false;
  }

  public function addEvent($name, $Fname){
    if(!empty($Fname)){
      self::$Events[$name][] = $Fname;
      return true;
    }
    return false;
  }

  public static function listEvents(){
    print '<pre>'; print_r (self::$Events); print '</pre>';
  }
}

// Each Class that wants Events would have to Extend the class

Class Test extends Events{
  function __construct(){
    $this->Var = 'test';
    $this->fireEvent('Onload');
  }
  function say(){
    $this->fireEvent('preSay');
    echo $this->Var;
    $this->fireEvent('Say');
  }
}

// Fire Functions

function Test_load($Obj){
  print 'Loaded Object:';
  print '<pre>';
  print_r ($Obj);
  print '</pre>';
  print '<hr>';
}
function Test_preSay($Obj){
  $Obj->Var = 'Welcome!';
  print 'Set Var as Welcome';
  print '<hr>';
}
function Test_Say1($Obj){
  print '<br>Finished Say';
  print '<hr>';
}
function Test_Say2($Obj){
  print 'Say 2!';
  print '<hr>';
}

//   Obj::addEvent( Event Name , Function Name );
Test::addEvent('Onload','Test_load');
Test::addEvent('preSay','Test_preSay');
Test::addEvent('Say','Test_Say1');
$Test = new Test();
// Works Also But Not for Onload
$Test->addEvent('Say','Test_Say2');
$Test->say();
$Test->listEvents();

/*
  Javscript does it like so..

  Test.addEvent('Say', function(){
    // Code
    // and you can use `this` (self Object)
  });

*/
Outputs:

Code: Select all

Loaded Object:<pre>Test Object
(
    [Var] => test
)
</pre><hr>Set Var as Welcome<hr>Welcome!<br>Finished Say<hr>Say 2!<hr><pre>Array
(
    [Onload] => Array
        (
            [0] => Test_load
        )

    [preSay] => Array
        (
            [0] => Test_preSay
        )

    [Say] => Array
        (
            [0] => Test_Say1
            [1] => Test_Say2
        )

)
</pre>
Right now, it only works for public Variables. The only way around it that i can think of is Either some how to Attach that Function to the Class.
Or Reference Each Variable In an Array, and then Pass the Array also with the Object as another Arg.

I did have the Referenced Array working, but it just seemed like to much dirty work to get protected/private variables, but then you still wouldn't have the functions.

Any input on this, is it just pointless? I could use some Evals, but i don't want to sacrifice speed.

It could be just me when i messed around with javascript alot for a couple weeks.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Right now, it only works for public Variables. The only way around it that i can think of is Either some how to Attach that Function to the Class.
There's no need to. You could pass all event-related information to event handler and make it use some well-defined public interface of the event source.

By the way, events are much more useful when they are registered per object, not a per class as in your implementation.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

But what is it for? Javascript has to deal with events from a long-lived interface. PHP uses a request/response cycle.
(#10850)
Post Reply