# PHP Namespaces

Namespaces are qualifiers that solve two different problems:
1.  Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
2.  Ability to alias (or shorten) Extra_Long_Names that are designed to alleviate the first problem.

Think of namespaces like directories in a file system. You can have `foo.txt` in directory A and `foo.txt` in directory B, and they won't conflict.

---

## 1. Defining a Namespace
Namespaces are declared at the very top of a file using the `namespace` keyword.

```php
<?php
namespace Html;

class Table {
    public $title = "";
    public $numRows = 0;
}
?>
```

## 2. Using Namespaces
If you are inside a namespace, you can access other classes in the same namespace directly. To access classes from outside, you must use the full path (starting with a backslash `\`).

```php
$table = new \Html\Table();
```

## 3. The `use` Keyword
You can import classes from other namespaces to avoid typing the full path every time.

```php
<?php
namespace Page;

use Html\Table;

$table = new Table();
?>
```

[[programming/php/php]]