1

I use zend_declare_class_constant_stringl macro to declare a constant property,but i don't konw how to read the constant? the declare code :

zend_declare_class_constant_stringl(myclass_ce,ZEND_STRL("WEL"),ZEND_STRL("welcome\n") TSRMLS_CC);

I want use zend_read_property or zend_read_static_property to read the constant property but it doesn't work!

(1): I use zend_read_static_property:

ZEND_METHOD(myclass,getName){
zval *name;
char *str;
zend_class_entry *ce;
ce=Z_OBJCE_P(getThis());
name=zend_read_static_property(ce,ZEND_STRL("name"),0 TSRMLS_CC);
str=Z_STRVAL_P(name);
RETURN_STRINGL(str,Z_STRLEN_P(name),1);
}

[root@localhost myext]# php -f /var/www/html/myclass.php

Notice: Undefined property: myclass::$WEL in /var/www/html/myclass.php on line 3
(null)PHP Fatal error:  Access to undeclared static property: myclass::$name in      /var/www/html/myclass.php on line 5

(2) I use zend_read_property:

ZEND_METHOD(myclass,getName){
zval *name;
char *str;
zend_class_entry *ce;
ce=Z_OBJCE_P(getThis());
name=zend_read_property(ce,getThis(),ZEND_STRL("name"),0 TSRMLS_CC);
str=Z_STRVAL_P(name);
RETURN_STRINGL(str,Z_STRLEN_P(name),1);
}

[root@localhost myext]# php -f /var/www/html/myclass.php

Notice: Undefined property: myclass::$WEL in /var/www/html/myclass.php on line 3
(null)silenceperPHP Fatal error:  Access to undeclared static property: myclass::$BYE in Unknown on line 0
silenceper
  • 15
  • 1
  • 5
  • 3
    A saying like *"it doesn't work!"* is a bit unspecific and it's not clear what you did expect actually and what happened instead. Care to share more? – hakre Aug 31 '13 at 14:41

1 Answers1

2

Use zend_get_constant_ex:

zval c;

if (zend_get_constant_ex(ZEND_STRL("ClassName::CONSTANT_NAME"), &c, NULL, 0 TSRMLS_CC) == SUCCESS) {
    // ...
}

The NULL is the class scope to perform the fetch from (if you want to use something like self). The 0 are flags, e.g. ZEND_FETCH_CLASS_SILENT.

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • like this: `ZEND_METHOD(myclass,__construct){ zval wel; zend_class_entry *ce; ce=Z_OBJCE_P(getThis()); zend_get_constant_ex(ZEND_STRL("self::WEL"),&wel,ce,0 TSRMLS_CC); php_printf("%s",Z_STRVAL(wel)); } ` Total 1 memory leaks detected – silenceper Sep 15 '13 at 02:40
  • @silenceper You have to `zval_dtor` the zval afterwards of course – NikiC Sep 15 '13 at 10:10
  • It was the `ClassName::` that was missing, so useful this answer, though I'd like to ask, is there a good resource about PHP extensions with c? – Iharob Al Asimi Jun 04 '15 at 22:32