Heim > Backend-Entwicklung > PHP-Tutorial > MordenPHP阅读笔记(一)——先跑再说,跑累了再走,mordenphp先跑_PHP教程

MordenPHP阅读笔记(一)——先跑再说,跑累了再走,mordenphp先跑_PHP教程

WBOY
Freigeben: 2016-07-12 08:53:56
Original
1213 Leute haben es durchsucht

MordenPHP阅读笔记(一)——先跑再说,跑累了再走,mordenphp先跑

---恢复内容开始---

  后台一大堆半成品,或者是几乎不成的。。。

这本书不错,起码是别人推荐的,然后也是比较新的东西,学哪本不是学嘛,关键是得看。

今儿个网不好,科研所需的代码下不到,看书做笔记吧。

这本书基本将的是5.4版本后的一些新变化,写的浅显易懂,虽然鄙人走的还不顺溜,跑一跑也摔不到哪儿去,跑累了我有的是走的机会~~

(一)特性

一、命名空间

一个文件一个类,用了命名空间方便互相调用;

<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>
Nach dem Login kopieren

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> }
Nach dem Login kopieren

二、使用接口

接口,本来没太懂,看懂了之后简直了,牛逼啊!

一个接口,大家只要遵守接口规定,就都能用,就这么个意思。

下面是一个获得内容的接口示例,还可以写更多基于此接口的模块;(其中,模块中getContent的我基本都不会。。。哭)

<?<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());
Nach dem Login kopieren

另一个模块

<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> }
Nach dem Login kopieren

三、性状

奇怪的东西。。。

其实就是为了多重继承或者一对多个不同的类别吧

<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();
Nach dem Login kopieren

四、生成器

直接上代码

<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>
Nach dem Login kopieren

五、闭包

这里闭包基本等于匿名函数

<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>);
Nach dem Login kopieren

六、附加状态

这个没搞懂。。。

(二)标准

PHP-FIG的一些约定俗成;

---类名称,驼峰式,ShitHappens

---方法名称,驼峰式,但首字母小写,shitHappens

---缩进统一为4个空格

---不写?>结束符号;

---{另起一行;

---命名空间要有空格;

---类中属性和方法必须有可见性声明;

---if等控制性结构后面有空格;

<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> }
Nach dem Login kopieren

 

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

后面的都是讲述的东西,有需要的我再写吧。 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1121987.htmlTechArticleMordenPHP阅读笔记(一)——先跑再说,跑累了再走,mordenphp先跑 ---恢复内容开始--- 后台一大堆半成品,或者是几乎不成的。。。 这本书不...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage