Bread, electricity and events for midgard-php

Posted on 2007-06-18 23:17:21 EEST.

Just commited latest fixes to generic connect method assigned to all MgdSchema classes. To those ones only , but it's very easy to add such method to all classes registered in midgard-php extension.

A bit of code instead of screenshot.

Create your own callbacks ( functions or methods ):

function testconnect($id = 0, $udata = 0)
{
        echo "Callback for event (parameters: ${id} AND ${udata}) \n";
}

class EventTest {

        function EventTest() {

        }

        function FetchEvent(&$param = 0) {

                echo "Got event with ${param} parameter. \n";
        }
}

My test function and test method are very simple. But the point here is to keep them simple. Once you have midgard-php working on your development machine, you can create very complicated callbacks :)

Let's create instances...

$et = new EventTest();
$t = new midgard_topic(1);
$art = new midgard_article(1);
$_b = 345;

...and connect callbacks to object which emmits signal.

$art->connect('action-updated', 'testconnect');
$art->connect('action-updated', 'testconnect', NULL, array(1));
$art->connect('action-updated', 'testconnect', NULL, array(3, &$_b));

Third parameter is explicitly set to NULL, as connect method expects object so ignores it if it's NULL.

$art->connect('action-update', 'FetchEvent', $et, array('cool'));
$art->connect('action-update', 'update', $t);

We set method name ( instead of function name ) when third parameter is an object. In both cases, fourth parameter must be an array and first one defines which event ( signal ) we want to connect to.

And the "application's" code:

$_b = 678;
$art->update();

And finally, the output:

midgard-core (pid:21523):(info): midgard_article::update(...)
Got event with cool parameter. 
midgard-core (pid:21523):(info): midgard_topic::update(...)
Callback for event (parameters: 0 AND 0) 
Callback for event (parameters: 1 AND 0) 
Callback for event (parameters: 3 AND 678)

As you noticed. Callback can be any function, and any class' method. MgdSchema or pure PHP one.

Have a nice events!

Back

Layout Copyright © 2006 Finnish Teleservice Center Ltd Oy - Site Powered by Midgard CMS