Home Backend Development PHP Tutorial Specific usage methods of namespace and use

Specific usage methods of namespace and use

May 17, 2018 pm 01:45 PM
namespace Instructions specific

namespace (hereinafter referred to as ns). After defining an ns, the class, interface, and const (excluding variables) declared below are all in the "domain" of the declared ns. When refers to a include file that declares ns, and if you want to call things in this ns, you must adjust the current script to this ns domain, otherwise you have to use the full name () to include it The full name of ns):

[php] view plain copy
// inc.php  
namespace Foo;  
class Bar {}  
// 访问Foo的第一种方法,用全称  
require 'inc.php';  
$foo = new \Foo\Bar();  
// 访问Foo的第二种方法  
namespace Foo; // 调整当前脚本到Foo这个ns域,而且namespace申明必须在第一句  
require 'inc.php';  
$foo = new Bar();
Copy after login

The purpose of the use keyword is to use the alias of ns:

[php] view plain copy
// 比如  
use A\Very\Long\Namespace as Ns;  
// 这样就可以用Ns来代替A/Very/Long/Namespace这个ns下定义的东西  
$foo = new Ns\Foo();
Copy after login

However, in some open source projects, we often see the usage of use Ns\Component without using as. This made me think about whether there is a second usage of use. The bad thing is that there is no description of this usage in the PHP documentation. I can only rely on guessing. Later, I thought about this problem carefully and came up with a more reliable one. The conclusion is that use can omit as and the following aliases and directly use the name of the last node of ns as an alias. Does it feel like the usage of the ln -s command:

[php] view plain copy
// 第三种用法  
require 'inc.php';  
use Foo\Bar; // 这样Bar就等于Foo\Bar了  
$foo = new Bar();
Copy after login

The above is what I compiled for everyone. The specific usage of namespace and use, I hope it will be helpful to everyone in the future.

Related articles:

Detailed usage method in PHP closure function() use()

PHP namespace Detailed usage guide for namespace and imported use

Detailed explanation of PHP Liskov substitution case

The above is the detailed content of Specific usage methods of namespace and use. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use DirectX repair tool? Detailed usage of DirectX repair tool How to use DirectX repair tool? Detailed usage of DirectX repair tool Mar 15, 2024 am 08:31 AM

How to use DirectX repair tool? Detailed usage of DirectX repair tool

Introduction to HTTP 525 status code: explore its definition and application Introduction to HTTP 525 status code: explore its definition and application Feb 18, 2024 pm 10:12 PM

Introduction to HTTP 525 status code: explore its definition and application

How to use Baidu Netdisk-How to use Baidu Netdisk How to use Baidu Netdisk-How to use Baidu Netdisk Mar 04, 2024 pm 09:28 PM

How to use Baidu Netdisk-How to use Baidu Netdisk

Learn to copy and paste quickly Learn to copy and paste quickly Feb 18, 2024 pm 03:25 PM

Learn to copy and paste quickly

What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool? What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool? Mar 18, 2024 am 11:07 AM

What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool?

What is PyCharm? Function introduction and detailed explanation of usage What is PyCharm? Function introduction and detailed explanation of usage Feb 20, 2024 am 09:21 AM

What is PyCharm? Function introduction and detailed explanation of usage

How to use Xiaoma win7 activation tool - How to use Xiaoma win7 activation tool How to use Xiaoma win7 activation tool - How to use Xiaoma win7 activation tool Mar 04, 2024 pm 06:16 PM

How to use Xiaoma win7 activation tool - How to use Xiaoma win7 activation tool

How to correctly use the win10 command prompt for automatic repair operations How to correctly use the win10 command prompt for automatic repair operations Dec 30, 2023 pm 03:17 PM

How to correctly use the win10 command prompt for automatic repair operations

See all articles