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
Next revisionBoth sides next revision
rfc:spl-namespace [2008/07/21 14:21] baptiste750rfc:spl-namespace [2008/07/21 15:29] baptiste750
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 61: Line 56:
 === Use with spl_autoload_register === === Use with spl_autoload_register ===
  
-TODO+The behaviour is the same, except that, just before executing the callback function, the parser **changes directory** to the physical path of the namespace. 
 + 
 +<code php> 
 +spl_namespace("Foo1::Foo2", "some/where"); 
 +(...) 
 +spl_autoload_register(array("Foo1::Foo2::Autoloader", "autoload")); 
 +// When Foo1::Foo2::Autoloader::autoload() is called, PHP has CD to /some/where  
 +</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 ====
Line 86: Line 90:
 ==== 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
  
   * ...   * ...
rfc/spl-namespace.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1