Home > Backend Development > PHP Tutorial > A preliminary study on PHP namespaces and automatic loading, a preliminary study on PHP namespaces_PHP tutorial

A preliminary study on PHP namespaces and automatic loading, a preliminary study on PHP namespaces_PHP tutorial

WBOY
Release: 2016-07-12 09:07:47
Original
760 people have browsed it

A preliminary study on PHP namespaces and automatic loading, a preliminary study on PHP namespaces

Reference materials: PHP Manual - Language Reference: http://php.net/manual/zh/language.namespaces.php ​ summary: 1. After the namespace is declared, the following const, function, and class will be classified into the namespace. 2. Only PHP files with a declared namespace can load PHP files with namespaces. 3. PHP 5.3 and above can use namespaces ​ ​ noun: Keyword: namespace is used to declare the namespace of this PHP file Constant: __NAMESPACE__ is used to return the name of the current namespace. The default is an empty string
Operator: use defaults to the string after the last one as the alias. If used with as, it will be the string after as, which is consistent with MySQL's field alias. The actual operation is as follows: Create the following files in the apache directory: index.php, Order.php, User.php ​ ​ The content of Order.php is ​ ​
<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 content is:

<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

The printed result is:

The above content includes: accessing files through namespace and direct instantiation access, as well as direct access in this space. The existence of namespace is to prevent two classes with the same name from being loaded. Using namespace can avoid conflicts of the same name when loading third-party classes. ​ Let’s talk about automatic loading ​ The full name of SPL is: Standard PHP Library. The PHP standard library has been built into PHP since PHP5 and does not require additional installation. SPL contains a set of function libraries for data structures, iterators, exceptions, file processing, etc. ​ The automatic loading library has the following functions spl_autoload_call: Try to call all registered __autoload() functions to load the request class

User.php content is:

<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

The printed result is:

SPL auto-loading functions include the following:

spl_autoload_extensions: Registers and returns the default file extensions used by the spl_autoload function.
get_include_path: Set the default referenced folder
spl_autoload_register: Automatically import files
The actual operation is as follows: We re-adjust the directory structure and copy order under lib as follows:

Modify User.php as follows:

<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

The printed result is:

Note: When using SPL to load a file, use cannot trigger the spl_autoload_register function. It will be triggered by new, which will prompt that the file cannot be found. All use spl_autoload_call to trigger automatic loading in advance. ​ URL of this article: http://www.cnblogs.com/martin-tan/p/4864539.html Question: When using get_include_path, spl_autoload_extensions and spl_autoload_register is empty by default, files in the directory cannot be loaded directly for the reasons above. (?) ​ ​

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1059465.htmlTechArticlePreliminary study on PHP namespace and automatic loading, preliminary study on php namespace Reference: PHP Manual-Language Reference: http:/ /php.net/manual/zh/language.namespaces.php Summary: 1. Declare the namespace...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template