And here is the final version of my class redeclaration patch.

class Foo {}

class Foo {}

-> Fatal error: Cannot redeclare class foo
(previously declared in /home/wez/source/php/PHPDEV/wez.declare.class.php:3)
in /home/wez/source/php/PHPDEV/wez.declare.class.php on line 5 

class Directory {}

-> Fatal error: Cannot redeclare built-in class directory in 
/home/wez/source/php/PHPDEV/wez.declare.class.php on line 3

--Wez.

Index: zend_API.h
===================================================================
RCS file: /repository/Zend/zend_API.h,v
retrieving revision 1.113
diff -u -r1.113 zend_API.h
--- zend_API.h  10 Sep 2002 15:33:45 -0000      1.113
+++ zend_API.h  29 Sep 2002 12:48:24 -0000
@@ -92,6 +92,7 @@
                class_container.handle_function_call = NULL;                           
 \
                class_container.handle_property_get = NULL;                            
         \
                class_container.handle_property_set = NULL;                            
         \
+               class_container.def_file = NULL;                        \
        }
 
 #define INIT_OVERLOADED_CLASS_ENTRY(class_container, class_name, functions, 
handle_fcall, handle_propget, handle_propset) \
@@ -102,6 +103,7 @@
                class_container.handle_function_call = handle_fcall;    \
                class_container.handle_property_get = handle_propget;   \
                class_container.handle_property_set = handle_propset;   \
+               class_container.def_file = NULL;                        \
        }
 
 
Index: zend_compile.c
===================================================================
RCS file: /repository/Zend/zend_compile.c,v
retrieving revision 1.237
diff -u -r1.237 zend_compile.c
--- zend_compile.c      26 Sep 2002 18:39:28 -0000      1.237
+++ zend_compile.c      29 Sep 2002 12:48:26 -0000
@@ -1180,18 +1180,33 @@
                        }
                        break;
                case ZEND_DECLARE_CLASS: {
-                               zend_class_entry *ce;
+                               zend_class_entry *ce, *exist_ce;
 
                                if (zend_hash_find(class_table, 
opline->op1.u.constant.value.str.val, opline->op1.u.constant.value.str.len, (void **) 
&ce)==FAILURE) {
                                        zend_error(E_ERROR, "Internal Zend error - 
Missing class information for %s", opline->op1.u.constant.value.str.val);
                                        return FAILURE;
                                }
+
+                               if (zend_hash_find(class_table, 
+opline->op2.u.constant.value.str.val, opline->op2.u.constant.value.str.len+1,
+                                                       (void **) &exist_ce) == 
+SUCCESS) {
+                                       if (!compile_time) {
+                                               if (exist_ce->def_file) {
+                                                       zend_error(E_ERROR, "Cannot 
+redeclare class %s (previously declared in %s:%d)",
+                                                               
+opline->op2.u.constant.value.str.val,
+                                                               exist_ce->def_file,
+                                                               exist_ce->def_lineno);
+                                               } else {
+                                                       zend_error(E_ERROR, "Cannot 
+redeclare built-in class %s",
+                                                               
+opline->op2.u.constant.value.str.val);
+                                               }
+                                       }
+                                       return FAILURE;
+                               }
+                               
                                (*ce->refcount)++;
                                if (zend_hash_add(class_table, 
opline->op2.u.constant.value.str.val, opline->op2.u.constant.value.str.len+1, ce, 
sizeof(zend_class_entry), NULL)==FAILURE) {
+                                       /* this case should already have been handled 
+above */
                                        (*ce->refcount)--;
-                                       if (!compile_time) {
-                                               zend_error(E_ERROR, "Cannot redeclare 
class %s", opline->op2.u.constant.value.str.val);
-                                       }
                                        return FAILURE;
                                } else {
                                        return SUCCESS;
@@ -1581,6 +1596,9 @@
        CG(class_entry).handle_property_set = NULL;
        CG(class_entry).handle_property_get = NULL;
 
+       CG(class_entry).def_file = estrdup(zend_get_compiled_filename(TSRMLS_C));
+       CG(class_entry).def_lineno = CG(zend_lineno);
+       
        /* code for inheritance from parent class */
        if (parent_class_name) {
                zend_class_entry *parent_class;
Index: zend_opcode.c
===================================================================
RCS file: /repository/Zend/zend_opcode.c,v
retrieving revision 1.56
diff -u -r1.56 zend_opcode.c
--- zend_opcode.c       6 Jan 2002 15:21:09 -0000       1.56
+++ zend_opcode.c       29 Sep 2002 12:48:26 -0000
@@ -115,6 +115,7 @@
                case ZEND_USER_CLASS:
                        efree(ce->name);
                        efree(ce->refcount);
+                       efree(ce->def_file);
                        zend_hash_destroy(&ce->function_table);
                        zend_hash_destroy(&ce->default_properties);
                        break;



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

Reply via email to