rfc:spl-namespace
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:spl-namespace [2008/07/21 11:52] – baptiste750 | rfc:spl-namespace [2008/07/22 08:41] – baptiste750 | ||
---|---|---|---|
Line 10: | Line 10: | ||
==== Introduction ==== | ==== Introduction ==== | ||
- | '' | + | The present RFC suggests a new namespacing system, based on the association |
- | + | ||
- | The default association being: Class Foo -> Foo.php | + | |
- | + | ||
- | Why not implementing a similar association between namespaces and physical paths? | + | |
- | The present RFC suggests a new namespacing system, based on that association. | + | |
==== A new instruction: | ==== A new instruction: | ||
- | '' | + | '' |
<code php> | <code php> | ||
- | spl_namespace(' | + | spl_namespace (' |
- | spl_namespace(' | + | spl_namespace (' |
</ | </ | ||
- | After a '' | + | === Use with spl_autoload === |
+ | |||
+ | After a '' | ||
<code php> | <code php> | ||
spl_namespace(' | spl_namespace(' | ||
... | ... | ||
- | spl_autoload(' | + | spl_autoload(' |
</ | </ | ||
- | But '' | + | But '' |
So, taking the example above, if MyClass.php contains: | So, taking the example above, if MyClass.php contains: | ||
Line 57: | Line 54: | ||
Note that PHP internal classes and absolute-namespaced classes ('' | Note that PHP internal classes and absolute-namespaced classes ('' | ||
- | ==== Benefits ==== | + | === Use with spl_autoload_register |
- | * The namespacing system becomes more consistent. It does no longer belong to the class (or the interface) to define its full qualifying name. It belongs to the element that requires it to assign its final position in the namespace. | + | The behaviour is the same, except that, just before executing |
- | + | ||
- | * Now that a namespace points to a clear and unique location, importing a namespace is unambiguous, | + | |
<code php> | <code php> | ||
- | use Foo1::Foo2::*; // or spl_use (?) | + | spl_namespace(" |
- | use Foo3; | + | (...) |
- | (..) | + | spl_autoload_register(array(" |
- | $a = MyClass(); | + | // When Foo1:: |
</ | </ | ||
- | The parser will look up in its autoloaded | + | In the example above, every class succesfully |
- | If only one MyClass on the three is found, it is used. | + | ==== Benefits ==== |
- | If two or the three MyClass are found, a fatal error is raised | + | |
+ | * The namespacing system becomes more consistent. It does no longer belong to the class (or the interface) to define its full qualifying name. It belongs to the element that requires it to assign its final position in the namespace. | ||
* It becomes possible to namespace old existing code without even modifying it. No need to add a '' | * It becomes possible to namespace old existing code without even modifying it. No need to add a '' | ||
Line 80: | Line 76: | ||
==== Drawbacks | ==== Drawbacks | ||
- | * You must use spl_autoload() | + | * If an autoloader, |
- | + | ||
- | To address that, an option could be added, in '' | + | |
* ... | * ... | ||
Line 99: | Line 93: | ||
spl_autoload(...); | spl_autoload(...); | ||
spl_autoload(...); | spl_autoload(...); | ||
- | ... // (this could form a common | + | ... // (or include files of autoloader |
spl_autoload(...); | spl_autoload(...); | ||
rfc/spl-namespace.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1