rfc:namespacecurlies
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:namespacecurlies [2008/08/31 12:29] – created helly | rfc:namespacecurlies [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Request for Comments: | + | ====== Request for Comments: |
* Version: 1.0 | * Version: 1.0 | ||
* Date: 2008-08-31 | * Date: 2008-08-31 | ||
Line 11: | Line 11: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | The following | + | During alpha phase of PHP 5.0 there was a namespace implementation that was very close to the one of C++. Because an inconsistency between namespace separator '::' |
- | - The keyword ' | + | |
+ | The following issues have been identified: | ||
+ | - The keyword ' | ||
- Namespace as kind of labels versus namespaces as blocks. | - Namespace as kind of labels versus namespaces as blocks. | ||
- Statements outside namespaces. | - Statements outside namespaces. | ||
- Nested namespace support. | - Nested namespace support. | ||
- | ==== The keyword ' | + | ==== The keyword ' |
The keyword has been discussed several times and even though a real explanation has not been found the agreement is that we think of namespaces literally. Furthermore the names are in no way related to the file system structure as is with Java packages for instance. The immediate conclusion is that we stick to the keyword. This also has the advantage that many people have seen namespace as a reserved keyword even when it was not a reserved keyword. This part is thus not open for discussion. | The keyword has been discussed several times and even though a real explanation has not been found the agreement is that we think of namespaces literally. Furthermore the names are in no way related to the file system structure as is with Java packages for instance. The immediate conclusion is that we stick to the keyword. This also has the advantage that many people have seen namespace as a reserved keyword even when it was not a reserved keyword. This part is thus not open for discussion. | ||
- | ==== Why do we need namespace | + | ==== Namespace as kind of labels versus namespaces as blocks ==== |
We need to describe how we think of namespaces. Right now the PHP runtime treats namespaces as simple text replacements. That is the runtime will only resolve names in the current and global namespace. With this in mind it does not matter whether we do namespaces as blocks or labels. This difference comes from experience, comparison to existing implementations, | We need to describe how we think of namespaces. Right now the PHP runtime treats namespaces as simple text replacements. That is the runtime will only resolve names in the current and global namespace. With this in mind it does not matter whether we do namespaces as blocks or labels. This difference comes from experience, comparison to existing implementations, | ||
Line 32: | Line 34: | ||
- Nearly all C++ editors are set to not indent on namespaces. | - Nearly all C++ editors are set to not indent on namespaces. | ||
- | | + | |
- Java and C++ are the closest, wide spread languages that support namespaces. | - Java and C++ are the closest, wide spread languages that support namespaces. | ||
- | - C++ uses the keywords ' | + | - C++ uses the keywords ' |
- Java uses the keywords ' | - Java uses the keywords ' | ||
Line 42: | Line 44: | ||
- With the new implementation and no blocks, many people expected one namespace per file. | - With the new implementation and no blocks, many people expected one namespace per file. | ||
- Since fewer files means faster execution, many people simply concatenate PHP scripts. Their expectation is to be able to do this for code that uses namespaces as well. | - Since fewer files means faster execution, many people simply concatenate PHP scripts. Their expectation is to be able to do this for code that uses namespaces as well. | ||
- | - PHP will add [[php.net/ | + | - PHP will add [[http://php.net/ |
* Consistency: | * Consistency: | ||
Line 48: | Line 50: | ||
- The alternative is to use ' | - The alternative is to use ' | ||
- Only control structures can be followed by a statement without either ';' | - Only control structures can be followed by a statement without either ';' | ||
+ | |||
+ | * Issues: | ||
+ | - When concatenating a file with a namespace and one without a namespace, the second file gets pulled into the namespace unless the namespace used curly braces. | ||
==== Statements outside namespaces ==== | ==== Statements outside namespaces ==== | ||
+ | |||
+ | PHP needs to support compiler caches and their way of name resolution. This is at the moment bound to filenames and as a consequence we cannot allow any code that potentially requires name lookup. This means we could only allow statements outside namespaces that either never do name lookup or we need special forms of those that disallow name lookup outside namespaces. In fact this seems only relevant to define() and include/ | ||
+ | |||
+ | Include as well as require do not carry over namespace information. This is a problematic as it allows for functions with mixed inner namespace resolution: | ||
+ | |||
+ | --foo.php | ||
+ | namespace foo; | ||
+ | function test{} { | ||
+ | echo __NAMESPACE__ . " | ||
+ | include ' | ||
+ | echo __NAMESPACE__ . " | ||
+ | } | ||
+ | |||
+ | --bar.php | ||
+ | // namespace bar; | ||
+ | echo __NAMESPACE__ . " | ||
+ | |||
+ | The above shows ' | ||
+ | |||
+ | We do not want to add special forms of require/ | ||
+ | |||
+ | This leaves us with define() as well as with require and include without carrying over the current namespace. For define we clearly allow name resolution: | ||
+ | |||
+ | $> php -r 'class C { const c = 42; } define(" | ||
+ | |||
+ | This in some way also applies to require and include because the both allow constants and variables. However define is a function and does not allow special treatment in the parser. Require and include on the other hand are parser states and take single expression as parameter. This expression can easily be turned into two different things. A full expression and a string only. The string only version can be allowed outside namespaces. | ||
==== Nested namespace support ==== | ==== Nested namespace support ==== | ||
+ | |||
+ | PHP uses simple text replacement for namespace resolution and thus can easily allow nested namespaces without any technical issue whatsoever. | ||
===== Proposal and Patch ===== | ===== Proposal and Patch ===== | ||
- | We propose to add namespaces as block structures, as in this [[http:// | + | We propose to add namespaces as block structures |
+ | |||
+ | In a second step nesting namespaces should be supported. This can easily be done by simply removing the corresponding error messages. | ||
===== Changelog ===== | ===== Changelog ===== |
rfc/namespacecurlies.1220185742.txt.gz · Last modified: 2017/09/22 13:28 (external edit)