<?php
use blah\blah;

$f = new blah;

namespace one {
use foo\bar as blah;
$a = new blah;
}

// what is this?
blah::hi();
?>

Technically, you could argue that blah::hi() should resolve to
blah\blah::hi(), but it is very difficult to track and figure out what
"blah" means by eye.  Thus, in the patch I implemented, if bracketed
namespace declarations exist, global use statements are not allowed, but
must exist within namespace ns {} brackets.

This creates a problem - how do you combine namespaced and unnamespaced
code?  To solve this, I introduced the oft-suggested "namespace {}"
...

Well, here's a cheesy mod of your example:

<?php
$f = 10;

function one() {
   $f = 20;
}

echo $f; // what is this? 10 ;)
?>

I guess it's clear that code below or above a namespace is in the same global namespace, so "use blah\blah" will apply ot "blah::hi();".

However, we do also need your suggestion: namespace [nothing] { ... } since one of the major design goals of the {} syntax variation was to allow people to merge multiple files for deployment. Something which several frameworks already do.

When we merge two files with "use" clauses, we need a way to provide "blank namespace" scope so the "use" applies only to the relevant part of the code.

We can't avoid your code example above either, since by default we don't require every piece of code to be in a namespace. If we allow {} scoping, it'll be awkward to suddenly require the remaining global code to be put in an explicit scope too, and people will be confused.

Hence a solution that seems most natural to me, and covers all use cases is the following (where the scope # tells you where the "use" declarations apply):

<?php
// global, scope 1

namespace { // global, scope 2 }

// global, scope 1

namespace { // global, scope 3 }

// global, scope 1

namespace foo\bar { // foo\bar, scope 4 }

// global, scope 1
?>

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

Reply via email to