Table of Contents
一、简介" >一、简介
二、 自动加载类库介绍" >二、 自动加载类库介绍
三、 例子" >三、 例子

三、 例子

Jun 13, 2016 pm 12:12 PM
class function psr public

遵循PSR-4的自动加载

一、简介

  首先这里要了解PSR,Proposing a Standards Recommendation(提出标准建议)的缩写,就是一种PHP开发规范,让我们研发出来的代码更合理、更好维护、可读性更高。PSR有下面几个标准:

  •   PSR-0:自动加载
  •   PSR-1:基本代码规范
  •     PSR-2:代码样式
  •   PSR-3:日志接口
  •   PSR-4:规范自动加载的路径问题

  这里看出PSR的下标也是从0开始的,和数组还有点像~。其实PSR-4和PSR-0是有点相似甚至冗余的,他们都说明的是自动加载的规范,只不过PSR-4中的规范更加简洁,在PSR-0中下划线"_"是有特殊含义的,在autoload处理的时候需要将下划线转换为目录分隔符,而在PSR-4中下划线是没有任何特殊含义的,所以在文件自动加载的时候显得更加简洁、调理更加清楚。

  我对github上面的psr-4规范中的例子进行了大概的翻译(相信你们的英语水平一定比我好,肯定可以看懂^_^),然后以这个自动加载类库做了一个小小的例子,例子文件多、长,放在这里不太合适,所以我在博客中就大概介绍下这个例子,想要详细了解的可以去我的github主页去看这个例子。

二、 自动加载类库介绍

  首先看下自动加载类的大概内容:

<span style="color: #0000ff;">class</span><span style="color: #000000;"> Autoload  {    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 注册自动加载函数到spl autoload栈中.</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> register();    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加一个目录到一个命名空间前缀中</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> addNamespace(<span style="color: #800080;">$prefix</span>, <span style="color: #800080;">$base_dir</span>, <span style="color: #800080;">$prepend</span>=<span style="color: #0000ff;">false</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 自动加载函数,会在$this->register中用到</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> loadClass(<span style="color: #800080;">$class</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 寻找映射的文件</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> loadMappedFile(<span style="color: #800080;">$prefix</span>, <span style="color: #800080;">$relative_class</span><span style="color: #000000;">);    </span><span style="color: #008000;">//</span><span style="color: #008000;">查看一个文件是否在文件系统中存在</span>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> requireFile(<span style="color: #800080;">$file</span><span style="color: #000000;">);  }</span>
Copy after login

  自动加载类库函数中就这几个函数,其中register()、addNamespace()、loadMappedFile()、requireFile()函数都比较简单,一看就懂,唯一一个可能需要解释下的函数就是loadClass函数,先看下loadClass()函数的代码:

<span style="color: #008080;"> 1</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> loadClass(<span style="color: #800080;">$class</span><span style="color: #000000;">)</span><span style="color: #008080;"> 2</span> <span style="color: #000000;">    {</span><span style="color: #008080;"> 3</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> 当前的命名空间前缀</span><span style="color: #008080;"> 4</span>         <span style="color: #800080;">$prefix</span> = <span style="color: #800080;">$class</span><span style="color: #000000;">;</span><span style="color: #008080;"> 5</span>         <span style="color: #008080;"> 6</span>         <span style="color: #008000;">//</span><span style="color: #008000;">通过命名空间去查找对应的文件</span><span style="color: #008080;"> 7</span>         <span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">false</span> !== <span style="color: #800080;">$pos</span> = <span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$prefix</span>, '\\'<span style="color: #000000;">)) {</span><span style="color: #008080;"> 8</span>             <span style="color: #008080;"> 9</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 可能存在的命名空间前缀</span><span style="color: #008080;">10</span>             <span style="color: #800080;">$prefix</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$class</span>, 0, <span style="color: #800080;">$pos</span> + 1<span style="color: #000000;">);</span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 剩余部分是可能存在的类</span><span style="color: #008080;">13</span>             <span style="color: #800080;">$relative_class</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$pos</span> + 1<span style="color: #000000;">);</span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span>             <span style="color: #008000;">//</span><span style="color: #008000;">试图加载prefix前缀和relitive class对应的文件</span><span style="color: #008080;">16</span>             <span style="color: #800080;">$mapped_file</span> = <span style="color: #800080;">$this</span>->loadMappedFile(<span style="color: #800080;">$prefix</span>, <span style="color: #800080;">$relative_class</span><span style="color: #000000;">);</span><span style="color: #008080;">17</span>             <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$mapped_file</span><span style="color: #000000;">) {</span><span style="color: #008080;">18</span>                 <span style="color: #0000ff;">return</span> <span style="color: #800080;">$mapped_file</span><span style="color: #000000;">;</span><span style="color: #008080;">19</span> <span style="color: #000000;">            }</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span>             <span style="color: #008000;">//</span><span style="color: #008000;"> 移动命名空间和relative class分割位置到下一个位置</span><span style="color: #008080;">22</span>             <span style="color: #800080;">$prefix</span> = <span style="color: #008080;">rtrim</span>(<span style="color: #800080;">$prefix</span>, '\\'<span style="color: #000000;">);   </span><span style="color: #008080;">23</span> <span style="color: #000000;">        }</span><span style="color: #008080;">24</span>         <span style="color: #008080;">25</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> 未找到试图加载的文件</span><span style="color: #008080;">26</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;</span><span style="color: #008080;">27</span>     }
Copy after login

  其实有疑惑的地方可能也只有一个,那就是为什么这里要循环着去试图查找文件,在while循环中,会慢慢的缩短命名空间前缀的名称去需找合适的命名空间前缀,为什么要这么做呢?

  循环查找文件是为了在命名空间中包含更多的内容,不用每次在父命名空间中新建一个文件夹的时候都去添加一个新的命名空间前缀,就像下面这个图中描述的那样:

  当一个文件在一个命名空间下的子目录下的时候,我们不用去新建命名空间前缀就可以成功加载需要的文件,维护命名空间前缀的数组内容更少,更好维护。相反的如果没有循环查找,就是下面这个样子的

  

  每次新建一个子目录就要去新加一个命名空间前缀,是不是很麻烦,但这样的话也有一定的好处,就是加载的时候不晕循环查找文件,可能会减小一定的时间消耗,但就是加载的时候有点麻烦。

  所以,用循环加载这种方式还是比较方便的,但是一定不能让没有命名空间前缀的目录层级太深,这样会消耗不必要的时间到文件加载上。当需要效率很高的时候,而我们的目录肯定又不会不确定,这个时候加载的时候去掉循环查找,而是为每个目录添加命名空间,效率可能会提高,只是我的一点愚见。

三、 例子

  说道这里你可能已经对自动加载的内容比较了解了,这个时候趁热打铁看看我准备的小例子,这里只是介绍下小例子的目录结构,由于比较简单,详细的内容就不再这里列了,感兴趣的通许可以去我的github主页看看这个例子

+autoload<br>    +core<br>        |_Autoload.php<br>    +vendor<br>        +test1<br>            |_hello.php<br>        +test2<br>            |_world.php<br>    |_App.php
Copy after login

  本文版权归作者([email protected])和博客园共有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。

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 to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What is the difference between the developer version and the public version of iOS? What is the difference between the developer version and the public version of iOS? Mar 01, 2024 pm 12:55 PM

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

See all articles