If I have a System folder in the root directory containing a Database folder with a Database.php file which holds a class
class Database{
static function Connect(){
echo "connect";
}
}
And I call that from an index.php which is in the root directory.
How do I make a namespace to access the class Database::Connect();
I really am struggling with namespaces.
Do I need to put namespace System\Database at the top of my Database.php file or something? Any good examples that aren’t on php.net pages?
Namespaces (in PHP) are really just a way of preventing naming collisions between Classes in a project. They have been used for a some time (before they were officially supported) in the form of classes named things like “Zend_Controller_Action_Helper”.
The PHP5.3 introduction of “real” namespaces really just means we can now use short, readable names in our code by “use”ing a namespace.
eg.
file: system/database.php
file public/index.php
file: public/index2.php
Namespaces are only related to directories by convention (and for autoloading) they, in an of themselves, don’t change the way classes are included and used.