3 ms·
I'm not up to date on php code, but can't the first example be done using reflection? For example a SubscriberBase class which: lists all of its "on*" methods,
by viraptor 6y ago
I'm not up to date on php code, but can't the first example be done using reflection? For example a SubscriberBase class which: lists all of its "on*" methods, checks if there's only one parameter, gets its type via https://www.php.net/manual/en/reflectionparameter.gettype.php https://www.php.net/manual/en/reflectionparameter.gettype.ph... and registers the method to receive events of that type.
From the docs it looks like it's possible in php7 and doesn't need explicit text mapping. But maybe I'm missing something?
- philjohn 6y agoI'd need to check if this is still the case in PHP, but Reflection was orders of magnitude slower just a few short years go for cases such as ReflectionClass->methodExists() vs method_exists()
- viraptor 6y agoDoes it matter if you execute it only once per request?
- deleted 6y ago[deleted]
- francislavoie 6y agoDefinitely possible, but that's a bit "magic". It's nicer not to have to rely on methods being named exactly a certain way. There was a discussion on reddit about this exact same thing, shows that attributes lets you bind multiple events to the same listener more easily: https://www.reddit.com/r/PHP/comments/jgkp2r/php_8_before_and_after_stitcherio/g9r8edt/?utm_source=reddit&utm_medium=web2x&context=3 https://www.reddit.com/r/PHP/comments/jgkp2r/php_8_before_an...