


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
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 stringOperator: 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

<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();
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();
The printed result is:
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');
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();
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. (?)

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software