Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example on how to use logger traits in an extension #43

Open
heyanlong opened this issue Aug 28, 2018 · 3 comments
Open

Add example on how to use logger traits in an extension #43

heyanlong opened this issue Aug 28, 2018 · 3 comments

Comments

@heyanlong
Copy link

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 
@jbboehr
Copy link
Owner

jbboehr commented Aug 28, 2018

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.

@heyanlong
Copy link
Author

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?

@jbboehr
Copy link
Owner

jbboehr commented Aug 28, 2018

@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 jbboehr changed the title How to build PsrLogLoggerTrait_ce_ptr to ce? Add example on how to use logger traits in an extension Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants