This patch allows php extension authors to declare constants on internal
objects. This pretty much just duplicates the zend_declare_property_*
functions but for constants. Currently there is no way for C level class
authors to declare constants on their objects since the engine has to
allocate space for them.

Bob Silva
diff -ur php5.orig/Zend/zend_API.c php5/Zend/zend_API.c
--- php5.orig/Zend/zend_API.c   2004-09-28 14:45:40.000000000 -0700
+++ php5/Zend/zend_API.c        2004-09-28 14:52:45.000000000 -0700
@@ -2037,6 +2037,67 @@
        return value;
 }
 
+ZEND_API int zend_declare_constant(zend_class_entry *ce, char *name, int name_length, 
zval *constant TSRMLS_DC)
+{
+
+       switch(Z_TYPE_P(constant)) {
+               case IS_ARRAY:
+               case IS_CONSTANT_ARRAY:
+               case IS_OBJECT:
+               case IS_RESOURCE:
+                       zend_error(E_CORE_ERROR, "Internal zval's can't be arrays, 
objects or resources");
+                       break;
+               default:
+                       break;
+       }
+
+       zend_hash_update(&(ce)->constants_table, name, name_length+1, &constant, 
sizeof(zval *), NULL);
+       return SUCCESS;
+}
+
+ZEND_API int zend_declare_constant_null(zend_class_entry *ce, char *name, int 
name_length TSRMLS_DC)
+{
+       zval *constant;
+
+       if (ce->type & ZEND_INTERNAL_CLASS) {
+               constant = malloc(sizeof(zval));
+       } else {
+               ALLOC_ZVAL(constant);
+       }
+       INIT_ZVAL(*constant);
+       return zend_declare_constant(ce, name, name_length, constant TSRMLS_CC);
+}
+
+ZEND_API int zend_declare_constant_long(zend_class_entry *ce, char *name, int 
name_length, long value TSRMLS_DC)
+{
+       zval *constant;
+
+       if (ce->type & ZEND_INTERNAL_CLASS) {
+               constant = malloc(sizeof(zval));
+       } else {
+               ALLOC_ZVAL(constant);
+       }
+       INIT_PZVAL(constant);
+       ZVAL_LONG(constant, value);
+       return zend_declare_constant(ce, name, name_length, constant TSRMLS_CC);
+}
+
+ZEND_API int zend_declare_constant_string(zend_class_entry *ce, char *name, int 
name_length, char *value TSRMLS_DC)
+{
+       zval *constant;
+       int len = strlen(value);
+
+       if (ce->type & ZEND_INTERNAL_CLASS) {
+               constant = malloc(sizeof(zval));
+               ZVAL_STRINGL(constant, zend_strndup(value, len), len, 0);
+       } else {
+               ALLOC_ZVAL(constant);
+               ZVAL_STRINGL(constant, value, len, 1);
+       }
+       INIT_PZVAL(constant);
+       return zend_declare_constant(ce, name, name_length, constant TSRMLS_CC);
+}
+
 /*
  * Local variables:
  * tab-width: 4
diff -ur php5.orig/Zend/zend_API.h php5/Zend/zend_API.h
--- php5.orig/Zend/zend_API.h   2004-09-28 14:45:40.000000000 -0700
+++ php5/Zend/zend_API.h        2004-09-28 14:46:04.000000000 -0700
@@ -195,6 +195,11 @@
 
 ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, char *name, 
int name_length, zend_bool silent TSRMLS_DC);
 
+ZEND_API int zend_declare_constant(zend_class_entry *ce, char *name, int name_length, 
zval *property TSRMLS_DC);
+ZEND_API int zend_declare_constant_null(zend_class_entry *ce, char *name, int 
name_length TSRMLS_DC);
+ZEND_API int zend_declare_constant_long(zend_class_entry *ce, char *name, int 
name_length, long value TSRMLS_DC);
+ZEND_API int zend_declare_constant_string(zend_class_entry *ce, char *name, int 
name_length, char *value TSRMLS_DC);
+
 ZEND_API zend_class_entry *zend_get_class_entry(zval *zobject TSRMLS_DC);
 
 #define getThis() (this_ptr)

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to