Actually tangled for a long time to do not use this thing, but look at the latest frame laravel,cakephp and so on the new version are used, so still follow the way to learn from the crowd.
namespace and C + + concepts are similar, but the usage is a little different.
First, if I want to use the MySQL class in the index.php file for the db/mysql.php file.
Here's how to use it:
mysql.php file:
namespace DB; class mysql{ public function Index () { 'im Mysql'; }}
index.php file
namespace test; // declares that the current file is under the test namespace. use db\mysql; // using the MySQL class under the DB namespace, just use DB;!!!! ' db/mysql.php '; // introduce mysql.php this file. new Mysql (); // so it can be used.
OK, continue, if you do not want to use XXXX, you can also directly $obj = new \db\mysql (), if you want to include mysql.php this file. There is a slight difference here.
$obj = new Mysql (); Find the MySQL class from the public area. (Public areas here refer to code that is not included in the namespace and system functions)
$obj = new Db\mysql (); Find the MySQL class from the DB space in the current space (this is the test). (the MySQL declaration that meets this requirement is namespace Test\db)
$obj = new \db\mysql (); Find the MySQL class for db space from the root of the namespace.
PHP namespace Usage