Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:12490 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22224 invoked by uid 1010); 31 Aug 2004 05:53:08 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 22189 invoked from network); 31 Aug 2004 05:53:07 -0000 Received: from unknown (HELO mta.ez.no) (194.248.150.25) by pb1.pair.com with SMTP; 31 Aug 2004 05:53:07 -0000 Received: from dhcp91.ez.no (stalker.ez.no [194.248.150.2]) by mta.ez.no (Postfix) with ESMTP id 5D85D48C0C4; Tue, 31 Aug 2004 08:35:50 +0200 (CEST) Organization: eZ systems To: internals@lists.php.net Date: Tue, 31 Aug 2004 07:53:04 +0200 User-Agent: KMail/1.6.1 Cc: Alan Knowles References: <4133F497.7080002@akbkhome.com> In-Reply-To: <4133F497.7080002@akbkhome.com> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-ID: <200408310753.05011.bf@ez.no> Subject: Re: [PHP-DEV] native events in PHP From: bf@ez.no (=?iso-8859-1?q?B=E5rd_Farstad?=) On Tuesday 31 August 2004 05:46, Alan Knowles wrote: > 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 =3D 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[] =3D 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=E8a to get into PHP in some way, however I = would=20 recommend that the signal/slot technology used in QT should be considered.= =20 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=20 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 =3D new MessageHandler(); $emailHandler =3D new MyEmailHandler(); $smsHandler =3D 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=E8a, but I think OO guys will like this. =2D-b=E5rd