]> granicus.if.org Git - php/commitdiff
Allowed multiple namespaces per file (Gregory)
authorDmitry Stogov <dmitry@php.net>
Thu, 13 Dec 2007 08:57:52 +0000 (08:57 +0000)
committerDmitry Stogov <dmitry@php.net>
Thu, 13 Dec 2007 08:57:52 +0000 (08:57 +0000)
NEWS
Zend/tests/ns_060.phpt [new file with mode: 0644]
Zend/zend_compile.c

diff --git a/NEWS b/NEWS
index 964e9d85df08a382e8f6b63532514c52236e37d1..a513f4709b2381edca155cac66b627509c0c3edc 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -16,7 +16,7 @@ PHP                                                                        NEWS
 - Added long-option feature to getopt() and made getopt() available also on 
   win32 systems by adding a common getopt implementation into core.
   (David Soria Parra, Jani)
-- Added support for namespaces. (Dmitry, Stas)
+- Added support for namespaces. (Dmitry, Stas, Gregory)
 - Added support for Late Static Binding. (Dmitry, Etienne Kneuss)
 - Added support for __callstatic() magic method. (Sara)
 - Added support for dynamic access of static members using $foo::myFunc().
diff --git a/Zend/tests/ns_060.phpt b/Zend/tests/ns_060.phpt
new file mode 100644 (file)
index 0000000..cc86d0e
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+060: multiple namespaces per file
+--FILE--
+<?php
+namespace Foo;
+use Bar::A as B;
+class A {}
+$a = new B;
+$b = new A;
+echo get_class($a)."\n";
+echo get_class($b)."\n";
+namespace Bar;
+use Foo::A as B;
+$a = new B;
+$b = new A;
+echo get_class($a)."\n";
+echo get_class($b)."\n";
+class A {}
+--EXPECT--
+Bar::A
+Foo::A
+Foo::A
+Bar::A
index f29aa567f6825820865cfa8d6511596a0d21438b..b4f3d88a03da8cd667ce462529d275b97e05cdd6 100644 (file)
@@ -4749,13 +4749,10 @@ void zend_do_namespace(znode *name TSRMLS_DC) /* {{{ */
                        CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
                        --num;
                }
-               if (num > 0) {
+               if (!CG(current_namespace) && num > 0) {
                        zend_error(E_COMPILE_ERROR, "Namespace declaration statement has to be the very first statement in the script");
                }
        }
-       if (CG(current_namespace)) {
-               zend_error(E_COMPILE_ERROR, "Namespace cannot be declared twice");
-       }
        lcname = zend_str_tolower_dup(Z_STRVAL(name->u.constant), Z_STRLEN(name->u.constant));
        if (((Z_STRLEN(name->u.constant) == sizeof("self")-1) &&
              !memcmp(lcname, "self", sizeof("self")-1)) ||
@@ -4765,7 +4762,17 @@ void zend_do_namespace(znode *name TSRMLS_DC) /* {{{ */
        }
        efree(lcname);
 
-       ALLOC_ZVAL(CG(current_namespace));
+       if (CG(current_namespace)) {
+               zval_dtor(CG(current_namespace));
+       } else {
+               ALLOC_ZVAL(CG(current_namespace));
+       }
+       if (CG(current_import)) {
+               zend_hash_destroy(CG(current_import));
+               efree(CG(current_import));
+               CG(current_import) = NULL;
+       }
+
        *CG(current_namespace) = name->u.constant;
 }
 /* }}} */
@@ -4884,7 +4891,7 @@ void zend_do_end_compilation(TSRMLS_D) /* {{{ */
 {
        if (CG(current_namespace)) {
                zval_dtor(CG(current_namespace));
-               efree(CG(current_namespace));
+               FREE_ZVAL(CG(current_namespace));
                CG(current_namespace) = NULL;
        }
        if (CG(current_import)) {