Oskar,
The new implementation of spl_object_hash will solve it, since it's a
faster implementation than the old one.
Just one hint, you should not try to echo the generated hash, since it
may have non-printable chars.
Also, your code may still be possible to use SplObjectStorage.
Etienne did a lot of research regarding how it could be used, and he
showed me that a n-dimensional array is possible too.
In your situation, $events should be an ArrayAccess instance, while
each $events[$event] should be an SplObjectStorage.
Something like that:
class EVENT {
private static $events = null;
public static function register($event, $obj, $method) {
if (self::$events === null) self::$events = new ArrayAccess();
if ( ! isset(self::$events[$event])) self::$events[$event] =
new SplObjectStorage();
self::$events[$event][$obj] = array($obj, $method);
}
public static function fire($event, &$eventData = null) {
if (isset(self::$events[$event])) {
foreach(self::$events[$event] as $callable) {
call_user_func_array($callable, array(&$eventData));
}
}
}
}
I hope this helps. =)
Cheers,
On Fri, Jan 23, 2009 at 4:23 PM, Oskar Eisemuth <[email protected]> wrote:
> Hello
>
> My usage for spl_object_id wouldn't be solved with SplObjectStorage,
> here is my current event handler (it uses spl_object_hash)
> I still have the plan to replace it with something better but it simply
> works,
> currently it's not possible to "free" an object.
>
> EVENT::register accepts a static class or object instance.
>
>
> class EVENT {
> private static $events = array();
>
> public static function register($event, $obj, $method) {
> if (!isset( self::$events[$event])) self::$events[$event] = array();
> if (is_object($obj)) {
> $hash = spl_object_hash($obj);
> } else {
> $hash = $obj;
> }
> self::$events[$event][$hash] = array($obj, $method);
> }
>
> public static function fire($event, &$eventData = null) {
> if (isset(self::$events[$event])) {
> foreach(self::$events[$event] as $callable)
> {
> call_user_func_array($callable, array(&$eventData));
> }
> }
> }
> }
>
> So spl_object_id would be a nice...
>
> Best regards
> Oskar Eisemuth
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9215-8480
MSN: [email protected]
URL: http://blog.bisna.com
São Paulo - SP/Brazil
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php