[php-src] Issue #11611: emulate C macro
| From: | SVGAnimate | Date: | Fri, 07 Jul 2023 07:27:33 +0000 |
| Subject: | [php-src] Issue #11611: emulate C macro | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244896@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11611
Author: SVGAnimate
### Description
Hello,
I would like to emulate this C [macro](https://docs.gtk.org/glib/func.DEFINE_QUARK.html) in a php
extension.
I clearly noted that the documentation indicates :
> This function is not directly available to language bindings.
But I want to be closer to C API and so be available to convert PHP code to C. This will allow me to
replace [GObject Builder](https://www.jirka.org/gob.html) with a powerful tool (php)
By this way, this code :
```php
<?php
G_DEFINE_QUARK ("QN", "q_n");
q_n_quark();
```
will be equivalent to :
```php
<?php
function q_n_quark() {
return "QN"
}
q_n_quark();
```
<hr/>
To achieve this I have extended zend_module_entry like this :
```C
typedef struct _php_g_module_entry {
zend_module_entry parent_instance;
struct _zend_function_entry *additional_functions;// Need to be freed
} php_g_module_entry;
```
So, instead of :
```C
zend_module_entry glib_module_entry = {
STANDARD_MODULE_HEADER,
"glib",
glib_functions,
PHP_MINIT(glib),
PHP_MSHUTDOWN(glib),
PHP_RINIT(glib),
PHP_RSHUTDOWN(glib),
PHP_MINFO(glib),
PHP_GLIB_VERSION,
STANDARD_MODULE_PROPERTIES
};
```
I write :
```C
php_g_module_entry glib_module_entry = {{
STANDARD_MODULE_HEADER,
"glib",
glib_functions,
PHP_MINIT(glib),
PHP_MSHUTDOWN(glib),
PHP_RINIT(glib),
PHP_RSHUTDOWN(glib),
PHP_MINFO(glib),
PHP_GLIB_VERSION,
STANDARD_MODULE_PROPERTIES
},
.additional_functions : NULL /* Holder of dynamic functions defined by G_DEFINE_QUARK() */
};
```
A call to
zend_register_functions() in PHP_FUNCTION(G_DEFINE_QUARK) and
you're done.
<hr/>
Is this the right way to do it ?
***Can you confirm I do not missed something ?***
<hr/>
> If eval() is the answer, you're almost certainly asking the
> wrong question. -- Rasmus Lerdorf, BDFL of PHP
```php
<?php
function G_DEFINE_QUARK ($QN, $q_n) {
eval("function {$q_n}_quark() {
return '$QN';
}");
}
G_DEFINE_QUARK('no warning in ZEND_GET_MODULE: initialization from incompatible pointer
type', 'q_n');
echo q_n_quark();
```