Giới thiệu
Có thể tạo namespace bên trong một namespace. Giống như thư mục trong hệ thống file có thể chứa thư mục con theo cấu trúc phân cấp, sub-namespace có thể được sắp xếp theo階層. Ký tự backslash \ dùng để định nghĩa mối quan hệ giữa namespace cấp cao nhất và cấp con.
Trong ví dụ này, namespace cấp cao nhất myspace chứa hai sub-namespace space1 và space2. Để truy cập function/class bên trong sub-namespace, trước hết hãy làm cho nó khả dụng bằng từ khóa use.
Ví dụ
<?php
namespace myspace\space1;
function hello() {
echo "Hello World from space1\n";
}
namespace myspace\space2;
function hello() {
echo "Hello World from space2\n";
}
use myspace\space1;
hello();
use myspace\space2;
hello();
?>
Kết quả
Hello World from space2 Hello World from space2
Lưu ý: Lệnh use gần nhất ghi đè lệnh use trước đó cho cùng tên function.