Table of Contents
MordenPHP reading notes (1) - run first and then talk, walk again when you are tired, mordenphp runs first
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

Jul 12, 2016 am 08:53 AM
one notes read

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Can deleted notes on Xiaohongshu be recovered? Can deleted notes on Xiaohongshu be recovered? Oct 31, 2023 pm 05:36 PM

Notes deleted from Xiaohongshu cannot be recovered. As a knowledge sharing and shopping platform, Xiaohongshu provides users with the function of recording notes and collecting useful information. According to Xiaohongshu’s official statement, deleted notes cannot be recovered. The Xiaohongshu platform does not provide a dedicated note recovery function. This means that once a note is deleted in Xiaohongshu, whether it is accidentally deleted or for other reasons, it is generally impossible to retrieve the deleted content from the platform. If you encounter special circumstances, you can try to contact Xiaohongshu’s customer service team to see if they can help solve the problem.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of &quot;What to do if the notes published by Xiaohongshu are missing&quot; and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click &quot;Me&quot; → &quot;Publish&quot; → &quot;All Publications&quot; to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to connect Apple Notes on iPhone in the latest iOS 17 system How to connect Apple Notes on iPhone in the latest iOS 17 system Sep 22, 2023 pm 05:01 PM

Link AppleNotes on iPhone using the Add Link feature. Notes: You can only create links between Apple Notes on iPhone if you have iOS17 installed. Open the Notes app on your iPhone. Now, open the note where you want to add the link. You can also choose to create a new note. Click anywhere on the screen. This will show you a menu. Click the arrow on the right to see the "Add link" option. click it. Now you can type the name of the note or the web page URL. Then, click Done in the upper right corner and the added link will appear in the note. If you want to add a link to a word, just double-click the word to select it, select "Add Link" and press

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

How to use Microsoft Reader Coach with Immersive Reader How to use Microsoft Reader Coach with Immersive Reader Mar 09, 2024 am 09:34 AM

In this article, we will show you how to use Microsoft Reading Coach in Immersive Reader on Windows PC. Reading guidance features help students or individuals practice reading and develop their literacy skills. You start by reading a passage or a document in a supported application, and based on this, your reading report is generated by the Reading Coach tool. The reading report shows your reading accuracy, the time it took you to read, the number of words correct per minute, and the words you found most challenging while reading. You will also be able to practice the words, which will help develop your reading skills in general. Currently, only Office or Microsoft365 (including OneNote for Web and Word for We

How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? Mar 25, 2024 pm 03:20 PM

As a lifestyle sharing platform, Xiaohongshu covers notes in various fields such as food, travel, and beauty. Many users want to share their notes on Xiaohongshu but don’t know how to do it. In this article, we will detail the process of posting notes on Xiaohongshu and explore how to block specific users on the platform. 1. How to publish notes tutorial on Xiaohongshu? 1. Register and log in: First, you need to download the Xiaohongshu APP on your mobile phone and complete the registration and login. It is very important to complete your personal information in the personal center. By uploading your avatar, filling in your nickname and personal introduction, you can make it easier for other users to understand your information, and also help them pay better attention to your notes. 3. Select the publishing channel: At the bottom of the homepage, click the &quot;Send Notes&quot; button and select the channel you want to publish.

realme GT Neo6 SE out-of-warranty repair price announced, screen replacement costs 580 yuan realme GT Neo6 SE out-of-warranty repair price announced, screen replacement costs 580 yuan Apr 17, 2024 pm 10:40 PM

The out-of-warranty repair prices for the just-released GTNeo6SE have been disclosed. The two most expensive components are the motherboard and screen. The starting price for out-of-warranty maintenance of the motherboard is 1,299 yuan, while the price of the screen assembly without dual screens is 580 yuan. What is particularly interesting is that the screen equipped by Realme GTNeo6SE is the world's first display with a brightness of up to 6,000 nits. Realme officially named it "unparalleled screen". This screen is the result of the cooperation between Realme and BOE. It uses the BOE S1 flexible screen specially customized for Realme. Its excellent peak brightness of 6000 nits sets a new industry standard. This screen also uses advanced 8TLTPO technology and supports a stepless adaptive refresh rate of 0.5-120Hz, which means that no matter what the user is watching,

See all articles