PHP Namespaces, what does use and \ means, and do they autoload classes?

namespaces, php

Solution

In response to your first three questions:

Namespacing !== Autoloading, namespacing is a way of simplifying your class structures, and allows "overloading" of classes in different namespaces; autoloading is automatically loading files when they are needed: they aren't the same thing. If you're using namespaces, you probably also want to use an autoloader as well.

A "leading" `\` is the global scope for namespacing; and subsequent `\` then serve as the namespace separator

`\` is a namespace separator; directory separators are `/` or `\` in filespecs depending on platform; but `/` works on all platforms anyway so should really be used for all cross-platform developments. There is also the `DIRECTORY_SEPARATOR` constant

Problem

I'm trying to educate myself with PHP 5.3/PHP 5.4 OOP features. I've tried to code something like this. It doesn't work, though. index.php ``` namespace Website; use Website\Database as Database; class Website extends Database { function __construct() { echo "Test"; } } $website = new Website(); ``` ./Website/Database.php ``` namespace Website\Database; class Database { function construct() { echo "Hello from Database"; } } ``` I know how to make classes, relate them to eachother etc. but whenever I add namespace to the top, everything gets broken. So I would like to ask a few elementary things; Q1: Does `use ClassName;` means it autoloads/includes the class? Q2: What does `\` means without anything on left side. (e.g new \Database();) Q3: Does `\` means a directory in PHP, or that is only how developers treat it as? Q4: What changes has to be done in my script to get it to work?

Original source