[imp] accessing $notify from within a hook

Michael M Slusarz slusarz at horde.org
Fri Jan 5 10:04:56 PST 2007


Quoting Liam Hoekenga <liamr at deathstar.org>:

> So, I'm playing around with IMP's "postlogin" hook, and want the  
> ability to display notices based on certain criteria.
>
> This works...
>
>    if (!function_exists('_imp_hook_postlogin')) {
>        function _imp_hook_postlogin($actionID, $isLogin)
>        {
>           $GLOBALS['notification']->push(_("this is a notice"),  
> 'horde.message');
>        }
>    }
>
> But, I'd wondered if I should try to use $notify instead of  
> $GLOBALS['notify'].  So I tried...
>
>    if (!function_exists('_imp_hook_postlogin')) {
>        function _imp_hook_postlogin($actionID, $isLogin)
>        {
>            require_once HORDE_BASE . '/lib/base.php';
>            $notification->push(_("this is a notice"), 'horde.message');
>        }
>    }
>
> ...which sort of looks like it works, but I see this in my error log...
>
>    [Fri Jan 05 12:42:04 2007] [error] [client 141.213.234.109]
>            script  
> '/usr/local/projects/webmail/html-ssl/horde/mailbox.php' not found  
> or unable to stat
>
> I tried adding this...
>
>        @define('IMP_BASE', dirname(__FILE__));
>        require_once IMP_BASE . '/lib/base.php';
>
> ...which didn't help.
>
> So, my question is, what's the best way to access $notify from  
> within a hook?  Is $GLOBALS['notify'] going to work for the  
> foreseeable future, or should I make the effort to get $notify into  
> my function's namespace (and if so, how do I do it?)?

This is what singletons are for.  So this should work now (and forever):

$notify = &Notification::singleton();
$notify->push(_("this is a notice"), 'horde.message');

Hooks are never called until the session/request is already set up, so  
you don't have to worry about including any of the base files.

michael

___________________________________
Michael Slusarz [slusarz at horde.org]



More information about the imp mailing list