Home > Backend Development > PHP Tutorial > 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_PHP tutorial

WBOY
Release: 2016-07-12 08:53:56
Original
1205 people have browsed it

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>
Copy after login

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> }
Copy after login

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());
Copy after login

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> }
Copy after login

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();
Copy after login

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>
Copy after login

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>);
Copy after login

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> }
Copy after login

------------------------

The following are all narrations, I will write more if necessary.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1121987.htmlTechArticleMordenPHP reading notes (1) - run first and talk later, walk when you are tired, mordenphp run first--- The restoration of content begins --- there are a lot of half-finished products in the background, or they are almost finished. . . This book is not...
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