Commit fb05427a authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Merge branch 'namespacing' into 'master'

Namespacing

- Namespaces
- Autoloading

See merge request !12
parents 8139f48c 77284572
......@@ -404,6 +404,61 @@ If the tags are on 1 line then indent with html
<strong>TEST: <?php echo htmlentities($test) ?></strong>
```
## 9. Namespaces with regards to Autoloading
### 9.1.1 Autoloading
- A fully-qualified namespace and class must have the following structure \<Vendor Name>\(<Namespace>\)*<Class Name>
- Each namespace must have a top-level namespace ("Vendor Name").
- Each namespace can have as many sub-namespaces as it wishes.
- Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.
- The fully-qualified namespace and class is suffixed with .php when loading from the file system.
- Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.
Example...
- \Doctrine\Common\IsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php
- \Symfony\Core\Request => /path/to/project/lib/vendor/Symfony/Core/Request.php
- \Zend\Acl => /path/to/project/lib/vendor/Zend/Acl.php
- \Zend\Mail\Message => /path/to/project/lib/vendor/Zend/Mail/Message.php
### 9.1.2 Autoloading Underscors in Namespaces and Class Names
- Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the
namespace.
Example...
- \namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php
- \namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php
### 9.1.3 Autoloading Example Implementation
Below is an example function to demonstrate how the above proposed standards are autoloaded
```php
<?php
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
```
## 9.2. Namespaces
### 9.2.1 Code written for PHP 5.3 and after MUST use formal namespaces.
Example...
```php
// PHP 5.3 and later:
namespace Vendor\Namespace
```
###
## 99. End Of File
To make life easier with tabs, files must contain
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment