


MordenPHP reading notes (1) - run first and then talk, walk again when you are tired, mordenphp runs first_PHP tutorial
MordenPHP reading notes (1) - run first and then talk, walk again when you are tired, mordenphp runs first
---Start restoring content---
There are a lot of semi-finished products in the background, or they are almost incomplete. . .
This book is good, at least it was recommended by others, and it is also relatively new. Learning any book is not learning, the key is to read it.
The Internet is not good today, and the code required for scientific research cannot be downloaded. Please read a book and take notes.
This book basically covers some of the new changes after version 5.4. It is written in an easy-to-understand manner. Although my walking is not smooth, I can’t fall anywhere even if I run. When I get tired from running, I have plenty of opportunities to walk. ~~
(1) Features
1. Namespace
One file and one class, using namespace to facilitate calling each other;
<span> 1</span> <span>//</span> <span> 2</span> <span>//Namespace </span><span> 3</span> <span>//</span> <span> 4</span> <span>namespace ModernPHP\feature\mingmingkongjian; </span><span> 5</span> <span>function</span> <span>var_dump</span><span>(){ </span><span> 6</span> <span>echo</span> "Shit!"."</br>"<span>; </span><span> 7</span> <span>} </span><span> 8</span> <span> 9</span> <span>$test</span>="OK"<span>; </span><span>10</span> <span>var_dump</span>(<span>$test</span><span>); </span><span>11</span> \ModernPHP\feature\mingmingkongjian\<span>var_dump</span><span>(); </span><span>12</span> <span>13</span> <span>//</span><span>命名空间必须顶头,但一个文件中可以有很多命名空间,然后也可以有子空间 </span><span>14</span> <span>//厂商的命名空间是最顶层的命名空间,用于识别品牌 </span><span>15</span> <span>//旨在解决命名冲突的问题,当然现在应该有比较灵活的其他用法 </span><span>16</span> <span>17</span> <span>//一个比较实用的点:导入和别名 </span><span>18</span> <span>//导入另一个文件夹下的类定义,直接用</span> <span>19</span> <span>require</span> 'index.php'<span>; </span><span>20</span> <span>use</span><span> a\aaa; </span><span>21</span> <span>$daoru</span>=<span>new</span><span> aaa; </span><span>22</span> <span>$daoru</span>-><span>send(); </span><span>23</span> <span>//</span><span>use是导入,然后在use中设置最懒的别名 </span><span>24</span> <span>//另外,5.6版本后可以实现use 函数 </span><span>25</span> <span>// use func a\call; </span><span>26</span> <span>// \a\call();</span>
index.php
<span> 1</span> <?<span>php </span><span> 2</span> <span>namespace a; </span><span> 3</span> <span>class</span><span> aaa{ </span><span> 4</span> <span>public</span> <span>function</span><span> send(){ </span><span> 5</span> <span>echo</span> "ok"<span>; </span><span> 6</span> <span> } </span><span> 7</span> <span>} </span><span> 8</span> <span> 9</span> <span>function</span><span> call(){ </span><span>10</span> <span>echo</span> "func_use is successful."<span>; </span><span>11</span> }
2. Use interface
I didn’t understand the interface very well at first, but after I understood it, it was awesome!
Everyone can use an interface as long as they comply with the interface regulations. That’s what it means.
The following is an example of an interface for obtaining content. You can also write more modules based on this interface; (Among them, I basically don’t know how to getContent in the module...crying)
<?<span>php </span><span>//</span><span> //Chapter2.P19 //Feature_Interface //</span> <span>namespace ModernPHP\feature\jiekou; </span><span>class</span><span> DocumentStore{ </span><span>protected</span> <span>$data</span>=<span>[]; </span><span>public</span> <span>function</span> addDocument(Documentable <span>$document</span>){ <span>//</span><span>这里注明只能使用接口的参数</span> <span>$key</span>=<span>$document</span>-><span>getID(); </span><span>$value</span>=<span>$document</span>-><span>getContent(); </span><span>$this</span>->data[<span>$key</span>]=<span>$value</span><span>; } </span><span>public</span> <span>function</span><span> getDocuments(){ </span><span>return</span> <span>$this</span>-><span>data; } } </span><span>interface</span> Documentable{ <span>//</span><span>定义接口,说白了就是定规矩,其他地方要用,就得说一声</span> <span>public</span> <span>function</span><span> getId(); </span><span>public</span> <span>function</span><span> getContent(); } </span><span>class</span> HtmlDocument <span>implements</span> Documentable{ <span>//</span><span>声明要用接口;这个是获得url的内容的</span> <span>protected</span> <span>$url</span><span>; </span><span>public</span> <span>function</span> __construct(<span>$url</span><span>){ </span><span>$this</span>->url=<span>$url</span><span>; } </span><span>public</span> <span>function</span><span> getId(){ </span><span>return</span> <span>$this</span>-><span>url; } </span><span>public</span> <span>function</span><span> getContent(){ </span><span>$ch</span>=curl_init(); <span>//</span><span>这里的curl是针对url进行操作一个库(相当于)。这个命令是开启一个curl对话,所以下面这些都是一个对话</span> curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>$this</span>-><span>url); curl_setopt(</span><span>$ch</span>, CURLOPT_RETURNTRANSFER, 1<span>); curl_setopt(</span><span>$ch</span>,CURLOPT_CONNECTTIMEOUT,3<span>); curl_setopt(</span><span>$ch</span>,CURLOPT_FOLLOWLOCATION,1<span>); curl_setopt(</span><span>$ch</span>,CURLOPT_MAXREDIRS,3<span>); </span><span>$html</span>=curl_exec(<span>$ch</span>); <span>//</span><span>由这个命令执行刚才的对话</span> curl_close(<span>$ch</span><span>); </span><span>return</span> <span>$html</span><span>; } } </span><span>$documentStore</span>=<span>new</span><span> DocumentStore(); </span><span>$htmlDoc</span>=<span>new</span> HtmlDocument('http://www.baidu.com'<span>); </span><span>$documentStore</span>->addDocument(<span>$htmlDoc</span><span>); </span><span>print_r</span>(<span>$documentStore</span>->getDocuments());
Another module
<span> 1</span> <span>class</span> StreamDocument <span>implements</span> Documentable{ <span>//</span><span>流媒体</span> <span> 2</span> <span>protected</span> <span>$resource</span><span>; </span><span> 3</span> <span>protected</span> <span>$buffer</span>; <span>//</span><span>缓冲区大小</span> <span> 4</span> <span> 5</span> <span>public</span> <span>function</span> __construct(<span>$resource</span>,<span>$buffer</span>=4096<span>){ </span><span> 6</span> <span>$this</span>-><span>resource</span>=<span>$resource</span><span>; </span><span> 7</span> <span>$this</span>->buffer=<span>$buffer</span><span>; </span><span> 8</span> <span> } </span><span> 9</span> <span>10</span> <span>public</span> <span>function</span><span> getId(){ </span><span>11</span> <span>return</span> 'resource-'.(int)<span>$this</span>-><span>resource</span><span>; </span><span>12</span> <span> } </span><span>13</span> <span>14</span> <span>public</span> <span>function</span><span> getContent(){ </span><span>15</span> <span>$streamContent</span>=''<span>; </span><span>16</span> <span>rewind</span>(<span>$this</span>-><span>resource</span>); <span>//</span><span>rewind() 函数将文件指针的位置倒回文件的开头</span> <span>17</span> <span>while</span> (<span>feof</span>(<span>$this</span>-><span>resource</span>)===<span>false</span>){ <span>//</span><span>feof() 函数检测是否已到达文件末尾 (eof)。</span> <span>18</span> <span>$streamContent</span>.=<span>fread</span>(<span>$this</span>-><span>resource</span>,<span>$this</span>-><span>buffer); </span><span>19</span> <span> } </span><span>20</span> <span>21</span> <span>return</span> <span>$streamContent</span><span>; </span><span>22</span> <span> } </span><span>23</span> }
3. Characteristics
Weird stuff. . .
In fact, it is for multiple inheritance or one pair of multiple different categories
<span> 1</span> <?<span>php </span><span> 2</span> <span>//</span> <span> 3</span> <span>//Chapter2.P23 </span><span> 4</span> <span>//Feature_Trait </span><span> 5</span> <span>//性状 </span><span> 6</span> <span>// </span><span> 7</span> <span> 8</span> <span>//前面说的接口,是针对同类型的东西,实现相同的功能的; </span><span> 9</span> <span>//这里的性状是针对不同的东西,实现相同的功能 </span><span>10</span> <span>11</span> <span>//基本用法如下</span> <span>12</span> <span>trait traitName{ </span><span>13</span> <span>public</span> <span>function</span><span> testThis(){ </span><span>14</span> <span>echo</span> "This is how trait works."."<br/>"<span>; </span><span>15</span> <span> } </span><span>16</span> <span>} </span><span>17</span> <span>18</span> <span>trait traitMore{ </span><span>19</span> <span>public</span> <span>function</span><span> testAgain(){ </span><span>20</span> <span>echo</span> "This is multiple use."."<br/>"<span>; </span><span>21</span> <span> } </span><span>22</span> <span>} </span><span>23</span> <span>24</span> <span>class</span><span> className{ </span><span>25</span> <span>use</span><span> traitName; </span><span>26</span> <span>use</span><span> traitMore; </span><span>27</span> <span>28</span> <span>} </span><span>29</span> <span>30</span> <span>$classMine</span>=<span>new</span><span> className(); </span><span>31</span> <span>$classMine</span>-><span>testThis(); </span><span>32</span> <span>$classMine</span>->testAgain();
4. Generator
Upload the code directly
<span> 1</span> <?<span>php </span><span> 2</span> <span>//</span> <span> 3</span> <span>//Chapter2.P26 </span><span> 4</span> <span>//Feature_Generator </span><span> 5</span> <span>//生成器 </span><span> 6</span> <span>// </span><span> 7</span> <span> 8</span> <span>//其实就是在函数中使用了yield语句的东西 </span><span> 9</span> <span>//优点在于节省了内存使用情况 </span><span>10</span> <span>//方法是通过动态分配内存进行循环操作 </span><span>11</span> <span>//典型用处是处理csv类数据文件</span> <span>12</span> <span>13</span> <span>namespace ModernPHP\feature\shengchegnqi; </span><span>14</span> <span>15</span> <span>function</span> getRows(<span>$file</span><span>){ </span><span>16</span> <span>$handle</span>=<span>fopen</span>(<span>$file</span>,'rb'<span>); </span><span>17</span> <span>if</span> (<span>$handle</span>===<span>false</span><span>){ </span><span>18</span> <span>throw</span> <span>new</span> <span>Exception</span>(); <span>//</span><span>抛出错误原因</span> <span>19</span> <span> } </span><span>20</span> <span>while</span> (<span>feof</span>(<span>$handle</span>)===<span>false</span><span>) { </span><span>21</span> yield <span>fgetcsv</span>(<span>$handle</span><span>); </span><span>22</span> <span> } </span><span>23</span> <span>fclose</span>(<span>$handle</span><span>); </span><span>24</span> <span>} </span><span>25</span> <span>26</span> <span>foreach</span> (getRows('data.csv') <span>as</span> <span>$row</span><span>){ </span><span>27</span> <span>print_r</span>(<span>$row</span><span>); </span><span>28</span> <span>echo</span> "<br/>"<span>; </span><span>29</span> <span>} </span><span>30</span> <span>//</span><span>当数据文件很大时,效果尤其明显</span>
5. Closure
The closure here is basically equivalent to an anonymous function
<span> 1</span> <?<span>php </span><span> 2</span> <span>//</span> <span> 3</span> <span>//Chapter2.P29 </span><span> 4</span> <span>//Feature_ClosePatch </span><span> 5</span> <span>//闭包或匿名函数 </span><span> 6</span> <span>// </span><span> 7</span> <span> 8</span> <span>//把函数当作是变量 </span><span> 9</span> <span>//然后它就可以像变量一样用来用去了。。 </span><span>10</span> <span>//常用做函数和方法的回调</span> <span>11</span> <span>12</span> <span>namespace ModernPHP\feature\bibao; </span><span>13</span> <span>$var</span>=<span>function</span> (<span>$name</span><span>){ </span><span>14</span> <span>return</span> <span>sprintf</span>('Hello %s',<span>$name</span><span>); </span><span>15</span> <span>}; </span><span>16</span> <span>17</span> <span>echo</span> <span>$var</span>('Andy'<span>); </span><span>18</span> <span>19</span> <span>//</span><span>做回调</span> <span>20</span> <span>$array</span>=[2,3,4<span>]; </span><span>21</span> <span>$num</span>=<span>array_map</span>(<span>function</span> (<span>$number</span>){ <span>//</span><span>array_map,将函数作用到数组中的每个值上,每个值都乘以本身,并返回带有新值的数组</span> <span>22</span> <span>return</span> <span>$number</span>+1<span>; </span><span>23</span> },<span>$array</span><span>); </span><span>24</span> <span>print_r</span>(<span>$num</span>);
6. Additional status
I don’t understand this. . .
(2) Standards
Some conventions of PHP-FIG;
---Class name, camel case, ShitHappens
---Method name, camel case, but the first letter is lowercase, shitHappens
---The indentation is unified to 4 spaces
---don’t write?> end symbol;
---{Start a new line;
---The namespace must have spaces;
---Attributes and methods in the class must have visibility declarations;
---if and other control structures are followed by spaces;
<span> 1</span> <?<span>php </span><span> 2</span> <span>//</span> <span> 3</span> <span>//Chapter3.P44 </span><span> 4</span> <span>//PHP-FIG puts PSRs </span><span> 5</span> <span>//</span> <span> 6</span> <span> 7</span> <span>namespace ModernPHP\standard\realize; </span><span> 8</span> <span> 9</span> <span>use</span><span> ModernPHP\feature\bibao; </span><span>10</span> <span>use</span><span> ModernPHP\feature\fujiazhuangtai; </span><span>11</span> <span>12</span> <span>class</span><span> ShitHappens </span><span>13</span> <span>{ </span><span>14</span> <span>public</span> <span>$a</span><span>; </span><span>15</span> <span>16</span> <span>public</span> <span>function</span><span> suck() </span><span>17</span> <span> { </span><span>18</span> <span>if</span> (<span>$this</span>->a===<span>false</span><span>){ </span><span>19</span> <span>return</span> <span>true</span><span>; </span><span>20</span> <span> } </span><span>21</span> <span> } </span><span>22</span> }
------------------------
The following are all narrations, I will write more if necessary.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)