Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: [PHP-DEV] [PATCH] allowing multiple namespaces per file plusnamespaces

0 views
Skip to first unread message

Gregory Beaver

unread,
Aug 21, 2007, 3:13:56 PM8/21/07
to
Stanislav Malyshev wrote:
>> What happens if you mix them?
>> <?php namespace baz;
>>
>> namespace foo {
>> class Bar {
>> ...
>> }
>> }
>> ?>
>
> That's not the fun yet. This is:
>
> <?php
> namespace baz;
>
> namespace foo { ...whatever... }
>
> class bar {}
>
> ?>

If by fun you mean "Fatal error: Namespace cannot be declared twice
(without brackets) or nested within brackets in ..."

To clarify, the patch works as follows:

when namespace baz; is encountered, CG(current_namespace) is set to a
non-NULL zstr (name->u.constant). Later on, when you try to namespace
foo {}, this check fails:

if (CG(current_namespace)) {
zend_error(E_COMPILE_ERROR, "Namespace cannot be declared twice
(without brackets) or nested within brackets");
}

In other words, if there is a current namespace present, you can't
declare another or redeclare. This code also fails with the same
E_COMPILE_ERROR above (and is one of the new tests in the patch):

<?php
namespace baz;
namespace foo;

class bar {}
?>

> Now, is bar in namespace baz or not? I guess it is, otherwise it stops
> making any sense. So now when you see in the code it says "class bar"
> what do you do to know which namespace it is? Right, you go up to the
> code, counting closing and open brackets and hoping you didn't miss any
> and try to figure out if it's that main namespace or one of the other
> ones. Now next question - how ones in namespace foo are supposed to call
> class bar? They'd have to call it by full name, baz::bar, right? Welcome
> back, long names. But wait, we could use an import, right? So now we'd
> have block-local imports. And we'd have to track them per
> namespace-block. BTW, what about imports in baz - are they active in
> foo? What if baz wants to talk to class in foo - should it import it?
> And BTW - how the patch solves it - I didn't read all of it, but not
> sure it does it correctly.
>
>> Would the compiler have a heart attack if someone did:
>>
>> <?php
>> namespace foo {
>> namespace baz {
>> class Bar { ... }
>> }
>> }
>> ?>
>
> This syntax implies that baz has access to names in foo. The separate
> syntax doesn't. But that's only part of the story - remember the
> names/imports story above? Now take that and make it unlimited depth
> stack, since you'd have to track everything on *each* nesting level
> separately. I wouldn't want to open this can of worms. That'd produce
> messy engine and messy code.

I don't want to open this can of worms either, and so I didn't. :)

Greg

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

Gregory Beaver

unread,
Aug 21, 2007, 4:15:27 PM8/21/07
to
David Zülke wrote:
> I think there should be just one way to designate a namespace, not two
> different ones with different behaviors.

Hi David,

First, the behaviors are not different, just the syntax. Of course you
can have more than one namespace per file with brackets, but the stuff
within that namespace reacts identically to the other format.

Second, if the consensus is one syntax, I'd say that's not really a big
deal. Doing so would simply involve removing one line from
zend_language_parser.y, rewording the error message, and dropping the
brackets parameters from zend_do_namespace's proto. For now, it's
better to investigate the ramifications of the patch first, as this
decision could be made quite easily closer to the first release.

Stanislav Malyshev

unread,
Aug 21, 2007, 4:48:45 PM8/21/07
to
>> <?php
>> namespace baz;
>>
>> namespace foo { ...whatever... }
>>
>> class bar {}
>>
>> ?>
>
> If by fun you mean "Fatal error: Namespace cannot be declared twice
> (without brackets) or nested within brackets in ..."


Ah, I didn't read it good enough - was under impression mixing them was
allowed.
--
Stanislav Malyshev, Zend Software Architect
st...@zend.com http://www.zend.com/
(408)253-8829 MSN: st...@zend.com

0 new messages