# multiple namespace per file patch

- Thread: https://externals.io/message/33666
- Original archive: https://news-web.php.net/php.internals/33666
- Started by Gregory Beaver on 2007-12-04T18:12:06+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #33666 — Gregory Beaver — 2007-12-04T18:12:06+00:00

URL: https://externals.io/message/33666#33666

Hi,

There's been enough confusion, here's the patch again, this time against
5.3, no tests for clarity

http://pear.php.net/~greg/multi.5.3.patch.txt

This patch allows:

<?php
namespace A;
class a {}
namespace B;
class a {}
namespace A;
import B::a as b;
class c extends b;
?>

It doesn't allow:
<?php
$anycode = 1;
namespace A;
?>

so all code must have a namespace declaration.  The sole purpose is to
combine files containing namespace declarations.

Greg

---

## #33692 — Sam Barrow — 2007-12-04T20:32:44+00:00

URL: https://externals.io/message/33666#33692 · In reply to: #33666

I support this patch 100%. I have a large application I'm rewriting to
include namespaces, however now I can no longer use my script caching
functionality (which improved performance literally about 500%) that
compiled all of my includes into one file, because many of the files
have namespaces.


> Hi,
> 
> There's been enough confusion, here's the patch again, this time against
> 5.3, no tests for clarity
> 
> http://pear.php.net/~greg/multi.5.3.patch.txt
> 
> This patch allows:
> 
> <?php
> namespace A;
> class a {}
> namespace B;
> class a {}
> namespace A;
> import B::a as b;
> class c extends b;
> ?>
> 
> It doesn't allow:
> <?php
> $anycode = 1;
> namespace A;
> ?>
> 
> so all code must have a namespace declaration.  The sole purpose is to
> combine files containing namespace declarations.
> 
> Greg
> plain text document attachment (multi.5.3.patch.txt)
> Index: Zend/zend_compile.c
> ===================================================================
> RCS file: /repository/ZendEngine2/zend_compile.c,v
> retrieving revision 1.647.2.27.2.41.2.28
> diff -u -r1.647.2.27.2.41.2.28 zend_compile.c
> --- Zend/zend_compile.c	4 Dec 2007 12:38:42 -0000	1.647.2.27.2.41.2.28
> +++ Zend/zend_compile.c	4 Dec 2007 17:24:34 -0000
> @@ -4722,13 +4722,10 @@
>  		        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)) ||
> @@ -4738,6 +4735,16 @@
>  	}
>  	efree(lcname);
>  
> +	if (CG(current_namespace)) {
> +		zval_dtor(CG(current_namespace));
> +		efree(CG(current_namespace));
> +	}
> +	if (CG(current_import)) {
> +		zend_hash_destroy(CG(current_import));
> +		efree(CG(current_import));
> +	}
> +	CG(current_import) = NULL;
> +
>  	ALLOC_ZVAL(CG(current_namespace));
>  	*CG(current_namespace) = name->u.constant;
>  }
