PHP namespace Usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.