Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the namespace Namespace in PHP

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use the namespace Namespace in PHP. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

This feature was proposed in PHP5.0x and was later cancelled and scheduled to be implemented in PHP6. And this time again "ahead of time" to the PHP5.3 release, which shows that developers attach importance to it and a cautious attitude.

The official release states that the contents of the document may be out of date (documentation maybe out dated), so here is a simple explanation of the use of namespaces: first, declare a namespace, adding the new keyword namespace, which should be at the beginning of the class file.

The copy code is as follows:

Then in the controller (possibly other files), you can call

The copy code is as follows:

$user = new Project::Module::User ()

$user- > register ($register_info)

It is true that it is the same as usual, but we can connect two independent classes. such as

The copy code is as follows:

Project::Module::User

Project::Module::Blog

This makes it easier to describe and understand the relationship between variables and classes from the language itself, thus avoiding the lengthy naming of the "traditional" Project_Module_Blog.

The above instructions may be difficult to illustrate the benefits of using namespaces, and the new use and as keywords may be more illustrative. Use and as statements can reference and declare "aliases" of namespaces. For example, the code for the instantiated class in the above controller can be written as follows

The copy code is as follows:

Use Project::Module

$user = new Module::User ()

$user- > register ($register_info)

Even

The copy code is as follows:

Use Project::Module::User as ModuleUser

$user = new ModuleUser

$user- > register ($register_info)

The constants in the class can also be accessed through the namespace, for example, STATUS_OK in the above class can be accessed through the namespace

The copy code is as follows:

Project::Module::User::STATUS_OK

Visit. Further, you can simplify such a long "variable name" with an alias.

The copy code is as follows:

Use Project::Module::User::STATUS_OK as STATUS_OK

Echo STATUS_OK

By the way, mention the concept of "The Global Namespace". The so-called "hyperspace" is variables, classes, and functions that do not specify a namespace. such as

The copy code is as follows:

Function foo () {

...

}

The function here can be executed using foo () as well as:: foo ();.

Finally, the class of the specified namespace can be loaded in conjunction with the autoload function. The simple functions are as follows

The copy code is as follows:

Function _ _ autoload ($classname) {

$classname = strtolower ($classname)

$classname = str_replace ('::', DIRECTORY_SEPARATOR, $classname)

Require_once (dirname (_ _ FILE__). '/'. $classname. '.class.php')

}

Like this, such as calling

The copy code is as follows:

_ _ autoload ('Project::Module::User')

You can load the Project_Module_User.class.php file automatically (although this doesn't seem convenient).

This is the end of this article on "how to use namespace Namespace in PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report