php-psr icon indicating copy to clipboard operation
php-psr copied to clipboard

Add example on how to use logger traits in an extension

Open heyanlong opened this issue 6 years ago • 3 comments

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 

heyanlong avatar Aug 28 '18 02:08 heyanlong

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.

jbboehr avatar Aug 28 '18 02:08 jbboehr

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 avatar Aug 28 '18 03:08 heyanlong

@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

jbboehr avatar Aug 28 '18 21:08 jbboehr