php-psr
php-psr copied to clipboard
Add example on how to use logger traits in an extension
e.g.
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, GUZZLE_NS, "Client", guzzle_client_method);
guzzle_client_ce = zend_register_internal_class(&ce);
zend_do_implement_trait(guzzle_client_ce, PsrLogLoggerTrait_ce_ptr);
zend_do_bind_traits(guzzle_client_ce); // zend_add_trait_method --> zend_arena_alloc error
Unfortunately, traits are not commonly used in extensions so I don't know what exactly needs to happen. I had to submit two PRs to php-src to get it to work at all [1] [2].
Additionally, it looks like zend_do_implement_trait
may have been removed in PHP 7.3 [3].
On the bright side, AFAIK, nothing a trait does can't be done the old-fashioned way, and this is a simple setter, so you can work around it easily enough.
static void message_trait_test(INTERNAL_FUNCTION_PARAMETERS) {
}
PHP_METHOD (MessageTrait, test) {
message_trait_test(execute_data, return_value);
}
PHP_METHOD (Client, test) {
message_trait_test(execute_data, return_value);
}
Is it the okay to does this?
@heyanlong I don't see why not? I think it's a pretty common pattern in PHP extensions where the function call overhead is a lot lower. I use it myself: https://github.com/jbboehr/php-handlebars/blob/9c834829671edd2c79707e986a7c26f06266b104/compiler.c#L485-L494