rfc:spl-namespace

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:spl-namespace [2008/07/21 14:44] baptiste750rfc:spl-namespace [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 2: Line 2:
   * Version: 1.0   * Version: 1.0
   * Date: 2008-07-19   * Date: 2008-07-19
-  * Author: Baptiste Autin <baptiste.autin.at.free.fr> +  * Author: Baptiste <fjas.at.free.fr> 
-  * Status: Under Discussion+  * Status: Abandonned
   * First Published at: http://wiki.php.net/rfc/spl-namespace   * First Published at: http://wiki.php.net/rfc/spl-namespace
   * Other formats ..   * Other formats ..
Line 10: Line 10:
 ==== Introduction ==== ==== Introduction ====
  
-''spl_autoload()'' is probably the first PHP instruction to make an assumption on how a logical element (a class) is bound to its source code (a file). +The present RFC suggests a new namespacing system, based on the association of a namespace to a physical path.
- +
-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: spl_namespace ==== ==== A new instruction: spl_namespace ====
  
-''spl_namespace($namespace, $path)'' binds a namespace to a physical path.+''spl_namespace($namespace, $path)'' declares the association of a namespace to a physical path.
  
 <code php> <code php>
-spl_namespace('PEAR::HTML') // links PEAR::HTML to PEAR/HTML +spl_namespace ('PEAR::HTML') // links PEAR::HTML to PEAR/HTML 
-spl_namespace('PEAR::HTML','some/where/else') // links PEAR::HTML to some/where/else+spl_namespace ('PEAR::HTML','some/where/else') // links PEAR::HTML to some/where/else
 </code> </code>
  
 === Use with spl_autoload === === Use with spl_autoload ===
  
-After a ''spl_namespace($namespace, $path)'', all subsequent ''spl_autoload(...)'' can refer to the namespace that has been defined in ''spl_namespace''.+After a ''spl_namespace($namespace, $path)'', all subsequent ''spl_autoload(...)'' that refer to $namespace will include the class file by changing directory to $path.
  
 <code php> <code php>
 spl_namespace('Foo1::Foo2', 'some/where'); spl_namespace('Foo1::Foo2', 'some/where');
 ... ...
-spl_autoload('Foo1::Foo2::MyClass');    // will include some/where/MyClass.php+spl_autoload('Foo1::Foo2::MyClass');    // this will include some/where/MyClass.php
 </code> </code>
  
-But ''spl_autoload'' will then not only require the file, it will ALSO prefix all the user classes/interfaces inside, with the namespace provided (''Foo1::Foo2::'').+But ''spl_autoload'' will then not only require the file, it will also **prefix** all the user classes/interfaces inside, with the namespace provided (''Foo1::Foo2::'').
  
 So, taking the example above, if MyClass.php contains: So, taking the example above, if MyClass.php contains:
Line 64: Line 59:
  
 <code php> <code php>
-spl_autoload_register(array('Foo1::Foo2::Autoloader''autoload')); +spl_namespace("Foo1::Foo2", "some/where"); 
-// When autoload is called, PHP has CD to /some/where +(...) 
 +spl_autoload_register(array("Foo1::Foo2::Autoloader""autoload")); 
 +// When Foo1::Foo2::Autoloader::autoload() is called, PHP has CD to /some/where 
 </code> </code>
 +
 +In the example above, every class succesfully autoloaded by ''Foo1::Foo2::Autoloader'' will be prefixed by ''Foo1::Foo2::'', as well as all the code inside that class.
  
 ==== Benefits ==== ==== Benefits ====
  
 * 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 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.
- 
-* Now that a namespace points to a clear and unique location, importing a namespace is unambiguous, and aliases are no longer needed: 
- 
-<code php> 
-use Foo1::Foo2::*; // or spl_use (?) 
-use Foo3; 
-(..) 
-$a = MyClass(); 
-</code> 
- 
-The parser will look up in its autoloaded classes for a ''Foo1::Foo2::MyClass'', for a ''Foo3::MyClass'' and then for a ''::MyClass'' (global namespace). 
- 
-If only one MyClass on the three is found, it is used. 
-If two or the three MyClass are found, a fatal error is raised (other scenario: the first ''MyClass'' found is taken, regardless of the rest). 
  
 * It becomes possible to namespace old existing code without even modifying it. No need to add a ''namespace'' at the beginning of every file. * It becomes possible to namespace old existing code without even modifying it. No need to add a ''namespace'' at the beginning of every file.
Line 91: Line 76:
 ==== Drawbacks  ==== ==== Drawbacks  ====
  
-  * ...+  * If an autoloader, for a given classname, includes a different file depending on the value of a global variable, there's no longer unicity
  
-  * ...+  * spl_autoload_register() will generally be present in the same file as the autoloader class, so changing directory is not really usefull, since we'll already be positionned in the correct directory.
  
 ==== Practical usage ==== ==== Practical usage ====
rfc/spl-namespace.1216651464.txt.gz · Last modified: 2017/09/22 13:28 (external edit)