Jump to content

create custom events?


aspnetguy

Recommended Posts

in C# I can easily create a custom event handler and fire it like this

public event EventHander myEvent;void myFunction() {  this.myEvent(this, EventArgs.Empty);}

Is there a way to do this in PHP? I am implementing a MVC/MVP pattern using interfaces (as I commonly do in C#) and would like to take an event based approach to executing code. Is this something I have to code myself or is there a built in EventHandler class?

Link to comment
Share on other sites

Just curious... how does this event model even work out? I can understand how it works in JavaScript since a connection between the script and the client firing the event is never lost, but I can't understand how it goes out with server languages.

Link to comment
Share on other sites

You set up the event handlers when the execution begins, and if any one of those events occurs while the script is running then the event handler gets fired. If the script ends without any of the events getting fired, then no change. You would also be able to manually trigger events, which would be similar to a function call.

Link to comment
Share on other sites

I see... hm... reminds me of XSLT's template model.That reminds me, aspnetguy, have you thought about using an XML file as a pipeline description, and then reading it out with the XMLReader class?Think of each node as an event that occurs, and the node's info as arguments for the callbacks. Example:

<?php$xml = new XMLReader;$xml->XML('<events><event1>event1 has already started, but has not ended yet. It ends with this elements' closing tag.</event1><event2>event1 has already started, but has not ended yet. It ends with this elements' closing tag.</event2></events>');while($xml->read()){switch($xml->nodeType) {  case XMLReader::TEXT:	//Things to do between the event start and end.	break;  case XMLReader::ELEMENT:	switch($xml->name) {	  case 'events':		//Things to do when the 'events' element starts.		break;	  case 'event1':		//Things to do when the 'event1' element starts.		break;	  case 'event2':		//Things to do when the 'event2' element starts.		break;	}	break;  case XMLReader::END_ELEMENT:	switch($xml->name) {	  case 'events':		//Things to do when the 'events' element ends.		break;	  case 'event1':		//Things to do when the 'event1' element ends.		break;	  case 'event2':		//Things to do when the 'event2' element ends.		break;	}	break;}}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...