Table of Contents
PHP命名空间和自动加载初探,php命名空间初探
Home php教程 php手册 PHP命名空间和自动加载初探,php命名空间初探

PHP命名空间和自动加载初探,php命名空间初探

Jun 13, 2016 am 08:53 AM
Namespaces

PHP命名空间和自动加载初探,php命名空间初探

参考资料: PHP手册-语言参考:http://php.net/manual/zh/language.namespaces.php   概要: 1. 声明了命名空间之后,下面的const, function, class都会划归到该命名空间。 2. 只有声明过命名空间的PHP 文件才能加载有命名空间的PHP文件。 3. PHP 5.3 及以上才能使用命名空间     名词:     关键字:namespace 用来声明 本PHP文件的命名空间     常量:__NAMESPACE__ 用来返回当前命名空间的名称 默认为空字符串
    操作符: use 默认以最后一个\后的字符串为别名,配合 as 则为 as后的字符串,与MySQL的字段别名一致。   实际操作如下: 在apache目录下建立如下文件:index.php,Order.php,User.php     Order.php 的内容为    
<span> 1</span> <?<span>php
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span> * @Author: Martin
</span><span> 4</span> <span> * @Support: Martin
</span><span> 5</span> <span> * @Last Modified by:   Martin
</span><span> 6</span>  <span>*/</span>
<span> 7</span> <span>namespace Order;
</span><span> 8</span> 
<span> 9</span> <span>const</span> STR = 'order list<br />'<span>;
</span><span>10</span> 
<span>11</span> <span>function</span><span> detail()
</span><span>12</span> <span>{
</span><span>13</span>     <span>return</span> 'order detail<br />'<span>;
</span><span>14</span> <span>}
</span><span>15</span> <span>function</span><span> call_by_self()
</span><span>16</span> <span>{
</span><span>17</span>     <span>return</span> 'call by self<br />'<span>;
</span><span>18</span> <span>}
</span><span>19</span> <span>/*</span><span>*
</span><span>20</span> <span> *
</span><span>21</span>  <span>*/</span>
<span>22</span> <span>class</span><span> Orderlist
</span><span>23</span> <span>{
</span><span>24</span> 
<span>25</span>     <span>public</span> <span>function</span><span> __construct()
</span><span>26</span> <span>    {
</span><span>27</span>         <span>echo</span> 'Class NameSpace is "', __NAMESPACE__, '"'<span>;
</span><span>28</span> <span>    }
</span><span>29</span>     <span>public</span> <span>function</span><span> show_list()
</span><span>30</span> <span>    {
</span><span>31</span>         <span>for</span> (<span>$i</span> = 0; <span>$i</span> < 5; <span>$i</span>++<span>) {
</span><span>32</span>             <span>echo</span> "<ul><li>this is order<span>$i</span><br />"<span>;
</span><span>33</span>             <span>//</span><span>内部直接访问</span>
<span>34</span>             <span>echo</span><span> detail();
</span><span>35</span>             <span>echo</span> "</li></ul>"<span>;
</span><span>36</span> <span>        }
</span><span>37</span> <span>    }
</span><span>38</span> <span>}
</span><span>39</span> <span>//</span><span>内部通过命名空间访问</span>
<span>40</span> <span>echo</span> \Order\call_by_self();
Copy after login

index.php 内容为:

<span> 1</span> <?<span>php
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span> * @Author: Martin
</span><span> 4</span> <span> * @Support: Martin
</span><span> 5</span> <span> * @Last Modified by:   Martin
</span><span> 6</span>  <span>*/</span>
<span> 7</span> <span>namespace index;
</span><span> 8</span> <span>include_once</span>('Order.php'<span>);
</span><span> 9</span> 
<span>10</span> <span>//</span><span>外部访问class 实例化即可使用</span>
<span>11</span> <span>use</span><span> Order\Orderlist;
</span><span>12</span> <span>$orderlist</span> = <span>new</span><span> orderlist;
</span><span>13</span> <span>$orderlist</span>-><span>show_list();
</span><span>14</span> 
<span>15</span> <span>//</span><span>外部访问静态变量和function 直接访问</span>
<span>16</span> <span>use</span><span> Order;
</span><span>17</span> <span>echo</span><span> Order\STR;
</span><span>18</span> <span>echo</span> Order\detail();
Copy after login

打印结果为:

以上内容包含了:通过命名空间来访问文件和直接实例化访问,以及本空间直接访问。 命名空间的存在是为了防止两个同名的class都被载入,使用命名空间在加载第三方的类时能避免同名冲突。 下面来说一下自动加载 SPL 的全称是:Standard PHP Library PHP标准库,在PHP5以后已经内置在PHP中,无需另外安装。 SPL包含了一套针对数据结构、迭代器、异常、文件处理等的函数库。 自动装载库有以下函数 spl_autoload_call:尝试调用所有已注册的__autoload()函数来装载请求类

User.php 内容为:

<span>1</span> <span>namespace User;
</span><span>2</span> <span>//</span><span>直接载入Order</span>
<span>3</span> <span>#</span><span>include('Order.php');
</span><span>4</span> <span>//自动载入</span>
<span>5</span> spl_autoload_register(<span>function</span>(<span>$className</span><span>) {
</span><span>6</span>     <span>var_dump</span>(<span>$className</span><span>);
</span><span>7</span> <span>});
</span><span>8</span> spl_autoload_call('Order');
Copy after login

打印结果为:

SPL自动载入函数包含如下:

spl_autoload_extensions: 注册并返回spl_autoload函数使用的默认文件扩展名。
get_include_path: 设置默认引用的文件夹
spl_autoload_register: 自动引入文件
实际操作如下: 我们重新调整目录结构和并复制order 到 lib下面 如下:

修改User.php 如下:

<span> 1</span> <span>namespace User;
</span><span> 2</span> 
<span> 3</span> <span>//</span><span>直接载入Order</span>
<span> 4</span> <span>#</span><span>include('Order.php');
</span><span> 5</span> <span>//自动载入</span>
<span> 6</span> <span>define</span>('LIB_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'lib' .<span> DIRECTORY_SEPARATOR);
</span><span> 7</span> spl_autoload_register(<span>function</span> (<span>$class</span><span>) {
</span><span> 8</span>     <span>$path</span> = LIB_DIR . <span>$class</span> . '.lib.php'<span>;
</span><span> 9</span>     <span>include</span> (<span>$path</span><span>);
</span><span>10</span> <span>});
</span><span>11</span> 
<span>12</span> spl_autoload_call('Order'<span>);
</span><span>13</span> <span>use</span><span> Order;
</span><span>14</span> 
<span>15</span> <span>$orderList</span> = <span>new</span><span> \Order\Orderlist();
</span><span>16</span> <span>$orderList</span>->show_list();
Copy after login

 

打印结果为:

 

注意: 当采用SPL载入文件时,use并不能触发spl_autoload_register函数,他会被new触发,这样就会提示找不到文件, 所有采用spl_autoload_call 来提前触发自动载入。   本文地址:http://www.cnblogs.com/martin-tan/p/4864539.html  问题: 使用get_include_path,spl_autoload_extensions并且spl_autoload_register默认为空的情况下并不能直接载入目录下的文件,原因如上。(?)    

 

 
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

Namespace configuration and application examples in PHP Namespace configuration and application examples in PHP Jun 25, 2023 am 08:32 AM

PHP is a highly flexible programming language with a wide range of applications. In PHP development, in order to avoid naming conflicts and improve the readability and maintainability of code, PHP introduces the concept of namespace. Namespaces help developers use the same class or function name in the same project without conflict. This article will introduce how to configure namespaces in PHP and common application examples. 1. How to configure the PHP namespace. Declare the namespace in PHP by using namespa at the top of the file.

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.

Methods to solve PHP namespace errors and generate corresponding error prompts Methods to solve PHP namespace errors and generate corresponding error prompts Aug 07, 2023 pm 05:16 PM

How to resolve PHP namespace errors and generate corresponding error messages. PHP is a widely used server-side scripting language that is used to develop web applications. In PHP, namespace (Namespace) is a mechanism for managing and organizing code, which can avoid naming conflicts and improve code readability and maintainability. However, the complexity of namespace definition and use sometimes leads to errors. This article will introduce some methods to solve PHP namespace errors and generate corresponding error prompts. 1. Name space

See all articles