Table of Contents
PHP命名空间概念解析,php命名空间解析
 1. PHP中的命名空间是什么?
  2. PHP命名空间该如何理解?
  3. PHP命名空间有何实际应用?
 4. 一些tips
Home php教程 php手册 PHP命名空间概念解析,php命名空间解析

PHP命名空间概念解析,php命名空间解析

Jun 16, 2016 am 09:16 AM
Namespaces

PHP命名空间概念解析,php命名空间解析

 1. PHP中的命名空间是什么?

  什么是命名空间?“从广义上来说,命名空间是一种封装事物的方法。在很多地方都可以见到这种抽象概念。例如,在操作系统中目录用来将相关文件分组,对于目录中的文件来说,它就扮演了命名空间的角色。具体举个例子,文件 foo.txt 可以同时在目录/home/greg 和 /home/other 中存在,但在同一个目录中不能存在两个 foo.txt 文件。另外,在目录 /home/greg 外访问 foo.txt 文件时,我们必须将目录名以及目录分隔符放在文件名之前得到 /home/greg/foo.txt。这个原理应用到程序设计领域就是命名空间的概念。”——命名空间概述

  2. PHP命名空间该如何理解?

  从本质上讲,命名空间就是一个容器,这个容器内我们可以放入类、函数和变量,他们在同一命名空间内可以无条件相互访问。在命名空间之外,就必须引用或者导入其他命名空间,才能调用它们包含的这些项。

  命名空间跟shell中的文件目录的概念是一样一样的。在当前目录下可以直接用文件名访问所有文件,如果需要访问其他目录下的文件,就需要输入相对路径或绝对路径。
引用方式:

namespace foo;

 class Foo {   

         public function foo()   

             {        

                  return \top\namespace\bar\Bar::fuck();    

              }

             }
Copy after login

  导入方式:

namespace foo; 

use top\namespace\bar\Bar; 

 class Foo {

        public function foo() 

            {        return Bar::fuck();  

            }

           }
Copy after login

  导入就相当于将目的类复制一份到当前命名空间中。

  3. PHP命名空间有何实际应用?

  命名空间的存在是为了解决下面两个问题:

  1. 用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/常量之间的名字冲突。

  2. 为很长的标识符名称(通常是为了缓解第一类问题而定义的)创建一个别名(或简短)的名称,提高源代码的可读性。 

 4. 一些tips

  1. 同一个空间下的类直接相互直接调用,属于一家。例如 Laravel 中的 PageController 类中可以直接写 Page::all() 这样的代码来调用 Page 这个model,因为他们俩都在顶级命名空间下。

  2. 若一个类存在于非顶级命名空间中,那么它只能在调用同样是当前命名空间下的其他类才不用“引用”或“导入”,它们属于一家。任何子命名空间都是另一个命名空间,另一个容器,没有除了容器之间关系之外的任何特殊关系。

  3. Laravel 采用 classmap 方式进行自动加载(autoload),PHP虽然有了命名空间这个高级特性,但是这只是逻辑关系,require 文件还是要有的。这个类和文件的对应关系就存在 /vendor/composer/autoload_classmap.php ,每次 composer dump-autoload 都会重新编译、生成。

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Solve PHP error: The specified namespace class was not found Solve PHP error: The specified namespace class was not found Aug 18, 2023 pm 11:28 PM

Solve PHP error: The specified namespace class was not found. When developing using PHP, we often encounter various error messages. One of the common errors is "The specified namespace class was not found". This error is usually caused by the imported class file not being properly namespace referenced. This article explains how to solve this problem and provides some code examples. First, let’s take a look at an example of a common error message: Fatalerror:UncaughtError:C

How to use namespace in F3 framework? How to use namespace in F3 framework? Jun 03, 2023 am 08:02 AM

The F3 framework is a simple, easy-to-use, flexible and scalable PHPWeb framework. Its namespace (Namespace) mechanism provides us with a more standardized, more readable, and clearer code structure. In this article, we will explore how to use namespaces in the F3 framework. 1. What is a namespace? Namespaces are often used to solve the problem of naming conflicts in PHP. It can encapsulate one or more classes, functions or constants in a namespace, which is equivalent to adding a prefix to them. example

Design ideas and implementation methods of Redis namespace and expiration mechanism Design ideas and implementation methods of Redis namespace and expiration mechanism May 11, 2023 am 10:40 AM

Redis is an open source, high-performance key-value storage database. When using Redis for data storage, we need to consider the design of the key namespace and expiration mechanism to maintain Redis performance and data integrity. This article will introduce the design ideas and implementation methods of Redis' namespace and expiration mechanism. 1. Redis namespace design ideas In Redis, keys can be set arbitrarily. In order to facilitate the management and distinction of different data types, Redis introduces the concept of namespace. Life

C++ syntax error: undefined namespace used, how to deal with it? C++ syntax error: undefined namespace used, how to deal with it? Aug 21, 2023 pm 09:49 PM

C++ is a widely used high-level programming language. It has high flexibility and scalability, but it also requires developers to strictly master its grammatical rules to avoid errors. One of the common errors is "use of undefined namespace". This article explains what this error means, why it occurs, and how to fix it. 1. What is the use of undefined namespace? In C++, namespaces are a way of organizing reusable code in order to keep it modular and readable. You can use namespaces to make functions with the same name

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Sep 11, 2023 pm 12:22 PM

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Introduction: PHP8 is an important version of the PHP programming language, which introduces many exciting new features and improvements. One of the most important new features is namespaces. Namespaces are a way to organize your code into a better structure that avoids conflicts between classes, functions, and constants with the same name. In this article, we’ll look at how to leverage namespaces and codes to better structure your PHP8 code

PHP 5.3 new feature: How to use namespaces to resolve class name conflicts PHP 5.3 new feature: How to use namespaces to resolve class name conflicts Jul 30, 2023 pm 12:25 PM

New features of PHP5.3: How to use namespaces to solve class name conflicts Introduction: During the development of PHP, as projects become larger and more complex, class name conflicts also arise. In order to solve this problem, PHP5.3 version introduced the concept of namespace. Namespaces provide a way to organize related classes, functions, and constants together to avoid naming conflicts. This article will introduce in detail the concept of PHP namespaces and how to use namespaces to solve class name conflicts, with code examples.

Best practices for PHP autoloading: ensuring stability and performance Best practices for PHP autoloading: ensuring stability and performance Mar 02, 2024 pm 09:10 PM

PHP Autoloading Overview Autoloading is a mechanism for automatically loading classes and their dependencies before use. In PHP, this is achieved by using the __autoload() function or an autoloader such as Composer. Proper autoloading settings are critical to ensuring the stability and performance of your codebase. PSR-4 automatic loading standard PSR-4 is the automatic loading standard defined by PHP-FIG. It is based on namespace and directory structure conventions to simplify class file lookup. To comply with PSR-4: define a root namespace (e.g. MyApp). Use backslash() as namespace separator. Use lowercase letters to represent namespace elements. Create a corresponding directory for each namespace element. General essay

PHP extension development: How to use namespaces to organize and manage custom functions? PHP extension development: How to use namespaces to organize and manage custom functions? Jun 04, 2024 pm 12:59 PM

It is crucial to manage custom functions using namespaces, which allow developers to create their own naming ranges and prevent name conflicts. The steps include: creating a namespace, using the use statement to import the namespace, and calling the namespace function. In a practical case, the MyMath extension demonstrates how to use namespaces to organize mathematical functions to improve readability and maintainability.

See all articles