aspnetguy Posted April 3, 2008 Share Posted April 3, 2008 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 More sharing options...
justsomeguy Posted April 3, 2008 Share Posted April 3, 2008 Unfortunately no, it doesn't look like PHP has support for an event-driven model like that. Here's hoping they add that to PHP6. You might be able to find something useful here:http://www.php.net/manual/en/ref.sem.phphttp://www.php.net/manual/en/control-struc...s.declare.ticks Link to comment Share on other sites More sharing options...
aspnetguy Posted April 3, 2008 Author Share Posted April 3, 2008 Thanks I'll just have to take a different approach. It was crucial, I just like coding that way. For me personally I produce less bugs when writing code like that. Link to comment Share on other sites More sharing options...
boen_robot Posted April 3, 2008 Share Posted April 3, 2008 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 More sharing options...
justsomeguy Posted April 3, 2008 Share Posted April 3, 2008 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 More sharing options...
boen_robot Posted April 3, 2008 Share Posted April 3, 2008 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 More sharing options...
aspnetguy Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks for the idea boen. I'll probaby just write a simple PHP class to do this similarly to how I do it in JavaScript Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.