# native events in PHP

- Thread: https://externals.io/message/12489
- Original archive: https://news-web.php.net/php.internals/12489
- Started by Alan Knowles on 2004-08-31T03:46:31+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #12489 — Alan Knowles — 2004-08-31T03:46:31+00:00

URL: https://externals.io/message/12489#12489

I've been looking at events in PHP, at present, in PEAR, there are lots 
of different methods to add callbacks, to packages, for various 
purposes. It seriously lacks cohesion, and would be nice to sort out at 
the language level.

Having seen C#, while not perfect, it is an interesting model.
There's a very short note on C#, and an idea for a PHP syntax.
http://www.akbkhome.com/wiki.php/DBDO/signals.html

It suggests:

class xxxx {
      // event registers a method, that hooks into the generic event 
handler.
      public event $onDebug = array();

     
      function somemethod() {

        // add a handler to an event..
    // this is the natrual syntax for this, but does open the door to 
someone wiping the stack!
        $this->onDebug[] = array($this,'mycallback');


    // initiate the signals. (function name matches the var!)
    $this->onDebug($x,$y);
     }
     // a handler..
     function mycallback($x,$y) {
    echo "debug hander got $x , $y\n";
     }
}


Anyone care to comment on
a) the feasibility
b) the "should it be implemented" question.
c) the "any better ideas" question...


Regards
Alan

---

## #12490 — bf@ez.no — 2004-08-31T05:53:04+00:00

URL: https://externals.io/message/12489#12490 · In reply to: #12489

> I've been looking at events in PHP, at present, in PEAR, there are lots
> of different methods to add callbacks, to packages, for various
> purposes. It seriously lacks cohesion, and would be nice to sort out at
> the language level.
>
> Having seen C#, while not perfect, it is an interesting model.
> There's a very short note on C#, and an idea for a PHP syntax.
> http://www.akbkhome.com/wiki.php/DBDO/signals.html
>
> It suggests:
>
> class xxxx {
>       // event registers a method, that hooks into the generic event
> handler.
>       public event $onDebug = array();
>
>
>       function somemethod() {
>
>         // add a handler to an event..
>     // this is the natrual syntax for this, but does open the door to
> someone wiping the stack!
>         $this->onDebug[] = array($this,'mycallback');
>
>
>     // initiate the signals. (function name matches the var!)
>     $this->onDebug($x,$y);
>      }
>      // a handler..
>      function mycallback($x,$y) {
>     echo "debug hander got $x , $y\n";
>      }
> }
>
>
> Anyone care to comment on
> a) the feasibility
> b) the "should it be implemented" question.
> c) the "any better ideas" question...

I think this would be a good idèa to get into PHP in some way, however I would 
recommend that the signal/slot technology used in QT should be considered. 
It's really simple and flexible to use.

Here is an example of how this could be done in PHP (not the best example, but 
it should show how it works ):

class MessageHandler
{
    public signal messageSent( $message );

    private function checkQueue()
    {
        foreach ( $messageQueue as $messsage )
        {
            // Send a signal for each message
            // So that the connected slots can handle it
            emit messageSent( $message );
        }
    }
}

class MyEmailMessageHandler
{
   public slot sendEmail( $message )
   {
        mail( $message );
   }
}

class MySMSMessageHandler
{
   public slots sendSMS( $message )
   {
       sendSMS( $message );
   }
}

When you instantiate your objects:

$messageHandler = new MessageHandler();
$emailHandler = new MyEmailHandler();
$smsHandler = new MySMSHandler();

// Connect is used to connect signals with slots.
// A signal can be used by multiple slots and a slot
// can have multiple signals connected to it.
connect( $messageHandler, 'messageSent', $emailHandler, 'sendEmail' );
connect( $messageHandler, 'messageSent', $emailHandler, 'sendSMS' );

There is some documentation on how that system works here:
http://doc.trolltech.com/3.3/signalsandslots.html

It's just an idèa, but I think OO guys will like this.

--bård

---

## #12508 — Al Baker — 2004-08-31T08:34:26+00:00

URL: https://externals.io/message/12489#12508 · In reply to: #12490

There's nothing stopping someone from writing their own classes for
dispatching and handling events and implementing callbacks today...

Are you suggesting something built into PHP like the Java system event
queue that people can define their own dispatchers and handlers for?
The QT example below is similar, connecting signals to slots instead of
dispatching events to listeners.

I guess it makes sense for long running scripts, like PHP-GTK. You
probably also want all those events processed in order by the same
system component (i.e. the system event queue). However, most web
applications have only one "event stimulus" at a time, i.e. the request
coming in from the web server.  They are then processed and go away in a
blink of an eye.

Hey, for PHP-GTK probably want to take it a step further and have some
standard event classes (WindowEvent, FocusEvent, etc).  Do these already
exist? -- haven't looked at PHP-GTK in much detail.

Al


> 
> > […]
> 
> I think this would be a good idèa to get into PHP in some way, however I would 
> recommend that the signal/slot technology used in QT should be considered. 
> It's really simple and flexible to use.
> 
> Here is an example of how this could be done in PHP (not the best example, but 
> it should show how it works ):
> 
> class MessageHandler
> {
>     public signal messageSent( $message );
> 
>     private function checkQueue()
>     {
>         foreach ( $messageQueue as $messsage )
>         {
>             // Send a signal for each message
>             // So that the connected slots can handle it
>             emit messageSent( $message );
>         }
>     }
> }
> 
> class MyEmailMessageHandler
> {
>    public slot sendEmail( $message )
>    {
>         mail( $message );
>    }
> }
> 
> class MySMSMessageHandler
> {
>    public slots sendSMS( $message )
>    {
>        sendSMS( $message );
>    }
> }
> 
> When you instantiate your objects:
> 
> $messageHandler = new MessageHandler();
> $emailHandler = new MyEmailHandler();
> $smsHandler = new MySMSHandler();
> 
> // Connect is used to connect signals with slots.
> // A signal can be used by multiple slots and a slot
> // can have multiple signals connected to it.
> connect( $messageHandler, 'messageSent', $emailHandler, 'sendEmail' );
> connect( $messageHandler, 'messageSent', $emailHandler, 'sendSMS' );
> 
> There is some documentation on how that system works here:
> http://doc.trolltech.com/3.3/signalsandslots.html
> 
> It's just an idèa, but I think OO guys will like this.
> 
> --bård

---

## #12510 — Alan Knowles — 2004-08-31T17:59:06+00:00

URL: https://externals.io/message/12489#12510 · In reply to: #12508

Actually, php-gtk has it's own connect/emit mechanism, what is 
problematic, is that a number of projects (both pear packages and 
others) utilize a callback/event model, and the only 2 ways to implement 
it are as a visitor type class, or recode the same simple method in a 
number of places.

In DBDO (my latest phpextension experiment), I wanted to be able to 
register callback handlers for debug logging, and it would have been 
nice to use an existing set of tools/API to deal with it. (in the end I 
copied code from the php-gtk extension)

Regards
Alan

Al Baker wrote:

>There's nothing stopping someone from writing their own classes for
>dispatching and handling events and implementing callbacks today...
>
>Are you suggesting something built into PHP like the Java system event
>queue that people can define their own dispatchers and handlers for?
>The QT example below is similar, connecting signals to slots instead of
>dispatching events to listeners.
>
>I guess it makes sense for long running scripts, like PHP-GTK. You
>probably also want all those events processed in order by the same
>system component (i.e. the system event queue). However, most web
>applications have only one "event stimulus" at a time, i.e. the request
>coming in from the web server.  They are then processed and go away in a
>blink of an eye.
>
>Hey, for PHP-GTK probably want to take it a step further and have some
>standard event classes (WindowEvent, FocusEvent, etc).  Do these already
>exist? -- haven't looked at PHP-GTK in much detail.
>
>Al
>
>
>  
>
> > […]
