Home Backend Development PHP Tutorial 简单概述PHP的命名空间及其在自动载入上的应用

简单概述PHP的命名空间及其在自动载入上的应用

Jun 20, 2016 pm 12:46 PM

php自5.3以后引入了命名空间namespace这一特性使得php在面向对象设计的过程中更加规范清晰,同时在框架的架构中自动载入模式也完全尽齐用来提高框架自身的性能--按需载入

无命名空间声明其实为在根命名空间下

<?php/*** 命名空间为Project* 则类的完整名为 Project\Web 而不是Web*/namespace Project;class Web{		function __construct()	{		echo __NAMESPACE__ . '<br/>';		echo __CLASS__ . '<br/>';	}}//若不已'\'开头则为使用当前命名空间下的类new Web();//当然我们可以指出完整的类名来 \Project\Web 为根命名空间下的Project命名空间下的Web类new \Project\Web();?>
Copy after login

1、若当前无命名空间声明,则默认在根命名空间'\'下

2、若当前存在命名空间生命,则默认的类实例化时都是此命名空间为前提,除非使用use声明某类的完整类名

比如

<?phpnamespace Web;use Common\Tools as CommonTools;class Tools {}//如同linux的路径//当前目录为 usr, 则 local 意味当前目录usr下的local,但 /var 则意味根目录下的 varnew Tools(); //从当前为命名空间Web开始,所以Tools的完整类名为Web\Toolsnew \Common\Tools(); //从根命名空间'\'开始,访问的是Common\Toolsnew CommonTools(); //因为使用了use声明,就如同将\Common\Tools 别名成 CommonTools?>
Copy after login

其实php的use声明并不是将此类导入到当前工作区域,而是单单指明

use Web\Tools;
Copy after login

Tools的类名是Web\Tools

当你new的时候他才会检查当前工作区中有没有这个类,没有的话就会检测有没有写自动载入函数

若也没有的话那就报错了,否则会调用你的自动载入函数

将你要实例化的完整类名传递进去

new Tools(); //这时会实例化名为Web\Tools的类,若没有include进来,且有些自动载入函数,则会将Web\Tools传递给次函数,你可以通过传递的类名做自己的引入处理
Copy after login

框架大都是根据类名做路径映射

比如Yii框架

//当前命名空间namespace app\Controllers;//声明Controller为yii\web命名空间下的,此后若有用到Controller的地方,完整名皆为yii\web\Controlleruse yii\web\Controller;//继承也会要求解析此类,所以当前工作目录会要求检查是否有yii\web\Controller的类,可惜没有//但系统会在给你一次几乎,看是否有自动加载函数,Yii当然有,传递给加载函数'yii\web\Controller'作为参数//加载函数解析类名发现是以yii开头的,则做路径映射,将yii\web\Controller映射为其框架类库文件的路径//即vendor\yiisoft\yii2, 拼接上web\Controller 拼接上.php后缀,就获取到了这个文件//文件里保存的类的命名空间为yii\web,类名为Controllerclass IndexController extends Controller {}
Copy after login



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)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles