Dmitry Stogov wrote:
> Looks fine, but probably we should emit error only if class declared in
> the same source file.
>
> Thanks. Dmitry.
>   

Great, didn't realize how easy this would be.  Attached patch does this,
but requires another test:

067.php.inc:
<?php
class B
{
    function __construct(){echo __CLASS__;}
}
?>

--TEST--
067: Name ambiguity (import name & userspace class name in another file)
--FILE--
<?php
include __DIR__ . '/066.php.inc';
include __DIR__ . '/067.php.inc';
use A::B;

new B();
--EXPECT--
A::B


I can't express how excited I am about this patch - it fixes the biggest
single problem with the "use" statement and negates the need for any
weird stuff like "namespace __user__;" as proposed.  Now, once we fix
resolution rules, namespaces will be stellar.

Greg
Index: Zend/zend_compile.c
===================================================================
RCS file: /repository/ZendEngine2/zend_compile.c,v
retrieving revision 1.647.2.27.2.41.2.68
diff -u -r1.647.2.27.2.41.2.68 zend_compile.c
--- Zend/zend_compile.c 15 Jun 2008 18:27:37 -0000      1.647.2.27.2.41.2.68
+++ Zend/zend_compile.c 20 Jun 2008 16:37:53 -0000
@@ -4959,6 +4959,7 @@
        char *lcname;
        zval *name, *ns, tmp;
        zend_bool warn = 0;
+       zend_class_entry **pce;
 
        if (!CG(current_import)) {
                CG(current_import) = emalloc(sizeof(HashTable));
@@ -5012,14 +5013,16 @@
                        efree(tmp);
                }
                efree(ns_name);
-       } else if (zend_hash_exists(CG(class_table), lcname, 
Z_STRLEN_P(name)+1)) {
-               char *tmp = zend_str_tolower_dup(Z_STRVAL_P(ns), 
Z_STRLEN_P(ns));
-
-               if (Z_STRLEN_P(ns) != Z_STRLEN_P(name) ||
-                       memcmp(tmp, lcname, Z_STRLEN_P(ns))) {
-                       zend_error(E_COMPILE_ERROR, "Cannot use %s as %s 
because the name is already in use", Z_STRVAL_P(ns), Z_STRVAL_P(name));
+       } else if (SUCCESS == zend_hash_find(CG(class_table), lcname, 
Z_STRLEN_P(name)+1, (void **)&pce)) {
+               if (pce[0]->type == ZEND_USER_CLASS && 
!strcmp(pce[0]->filename, CG(compiled_filename))) {
+                       char *tmp = zend_str_tolower_dup(Z_STRVAL_P(ns), 
Z_STRLEN_P(ns));
+       
+                       if (Z_STRLEN_P(ns) != Z_STRLEN_P(name) ||
+                               memcmp(tmp, lcname, Z_STRLEN_P(ns))) {
+                               zend_error(E_COMPILE_ERROR, "Cannot use %s as 
%s because the name is already in use", Z_STRVAL_P(ns), Z_STRVAL_P(name));
+                       }
+                       efree(tmp);
                }
-               efree(tmp);
        }
 
        if (zend_hash_add(CG(current_import), lcname, Z_STRLEN_P(name)+1, &ns, 
sizeof(zval*), NULL) != SUCCESS) {

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

Reply via email to