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>
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> }
二、使用接口
接口,本来没太懂,看懂了之后简直了,牛逼啊!
一个接口,大家只要遵守接口规定,就都能用,就这么个意思。
下面是一个获得内容的接口示例,还可以写更多基于此接口的模块;(其中,模块中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());
另一个模块
<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> }
三、性状
奇怪的东西。。。
其实就是为了多重继承或者一对多个不同的类别吧
<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();
四、生成器
直接上代码
<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>
五、闭包
这里闭包基本等于匿名函数
<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>);
六、附加状态
这个没搞懂。。。
(二)标准
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> }
----------------------
后面的都是讲述的东西,有需要的我再写吧。

小红书笔记怎么删除?在小红书APP中是可以编辑笔记的,多数的用户不知道小红书笔记如何的删除,接下来就是小编为用户带来的小红书笔记删除方法图文教程,感兴趣的用户快来一起看看吧!小红书使用教程小红书笔记怎么删除1、首先打开小红书APP进入到主页面,选择右下角【我】进入到专区;2、之后在我的专区,点击下图所示的笔记页面,选择要删除的笔记;3、进入到笔记页面,右上角【三个点】;4、最后下方会展开功能栏,点击【删除】即可完成。

小红书删除的笔记不能恢复。小红书作为一款知识分享和购物平台,为用户提供了记录笔记和收藏有用信息的功能。根据小红书的官方说明,已经删除的笔记是无法恢复的。小红书平台并没有提供专门的笔记恢复功能。这意味着,一旦在小红书中删除了笔记,无论是不小心误删还是其他原因,一般情况下是无法从平台上找回被删除的内容的。如果遇到特殊情况,可以尝试联系小红书的客服团队,看是否能够协助解决问题。

作为一名小红书的用户,我们都曾遇到过发布过的笔记突然不见了的情况,这无疑让人感到困惑和担忧。在这种情况下,我们该怎么办呢?本文将围绕“小红书发布过的笔记不见了怎么办”这一主题,为你详细解答。一、小红书发布过的笔记不见了怎么办?首先,不要惊慌。如果你发现笔记不见了,保持冷静是关键,不要慌张。这可能是由于平台系统故障或操作失误引起的。检查发布记录很简单。只需打开小红书App,点击“我”→“发布”→“所有发布”,就可以查看自己的发布记录。在这里,你可以轻松找到之前发布的笔记。3.重新发布。如果找到了之

使用添加链接功能在iPhone上链接AppleNotes。笔记:如果您已安装iOS17,则只能在iPhone上的AppleNotes之间创建链接。在iPhone上打开“备忘录”应用。现在,打开要在其中添加链接的注释。您还可以选择创建新备忘录。点击屏幕上的任意位置。这将向您显示一个菜单。点击右侧的箭头以查看“添加链接”选项。点击它。现在,您可以键入注释的名称或网页URL。然后,点击右上角的完成,添加的链接将出现在笔记中。如果要添加指向某个单词的链接,只需双击该单词即可将其选中,选择“添加链接”并按

小红书怎么在笔记中添加商品链接?在小红书这款app中用户不仅可以浏览各种内容还可以进行购物,所以这款app中关于购物推荐、好物分享的内容是非常多的,如果小伙伴在这款app也是一个达人的话,也可以分享一些购物经验,找到商家进行合作,在笔记中添加连接之类的,很多人都愿意使用这款app购物,因为不仅方便,而且有很多达人会进行一些推荐,可以一边浏览有趣内容,一边看看有没有适合自己的衣服商品。一起看看如何在笔记中添加商品链接吧!小红书笔记添加商品链接方法 在手机桌面上打开app。 在app首页点击

在这篇文章中,我们将向你展示如何在WindowsPC上的沉浸式阅读器中使用Microsoft阅读教练。阅读指导功能帮助学生或个人练习阅读并培养他们的识字技能。你从阅读支持的应用程序中的一段或一份文档开始,基于此,你的阅读报告由阅读教练工具生成。阅读报告显示了阅读的准确性、阅读所用的时间、每分钟的正确单词数,以及你在阅读时发现最具挑战性的单词。你还将能够练习这些单词,这总体上有助于培养你的阅读技能。目前,仅有Office或Microsoft365(包括OneNoteforWeb和WordforWe

小红书作为一个生活方式分享平台,涵盖了美食、旅行、美妆等各个领域的笔记。许多用户希望在小红书上分享自己的笔记,但却不清楚如何操作。在这篇文章中,我们将详细介绍小红书发布笔记的流程,并探讨如何在平台上屏蔽特定用户。一、小红书发布笔记教程怎么弄?1.注册登录:首先,需要在手机上下载小红书APP,并完成注册登录。在个人中心完善个人资料是很重要的。通过上传头像、填写昵称和个人简介,可以让其他用户更容易了解你的信息,也能帮助他们更好地关注你的笔记。3.选择发布频道:在首页下方,点击“发笔记”按钮,选择你想

刚刚发布的GTNeo6SE的保外维修价格已经公开,其中最为昂贵的两个部件是主板和屏幕。主板的保外维修起价为1299元,而无双屏的屏幕组件的价格则为580元。特别引人关注的是,真我GTNeo6SE配备的屏幕是全球首款亮度高达6000尼特的显示屏,真我官方将其命名为"无双屏"。这块屏幕是真我与京东方合作的成果,采用了专门为真我定制的京东方S1柔性屏,其卓越的6000尼特峰值亮度树立了新的行业标准。这块屏幕还采用了先进的8TLTPO技术,支持0.5-120Hz的无级自适应刷新率,这意味着无论用户是在观


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
