MordenPHP Reading Notes (1) -- run first, run tired, and then run mordenphp first
--- Restore content start ---
There are a lot of semi-finished products in the background, or they are almost impossible...
This book is good. At least it is recommended by others, and then it is quite new. The key to learning which one is not learning is to read it.
Today, the Internet is not good, and the code required for scientific research is not available. Read and take notes.
This book basically involves some new changes after version 5.4, which are easy to understand. Even though you are not easy to go, you can't go anywhere when you run it, when I get tired, I have a chance to go ~~
(1) Features
I. namespace
A file is a class and namespace is used for mutual calls;
1 // 2 // Namespace 3 // 4 namespace ModernPHP \ feature \ mingmingkongjian; 5 function var_dump () {6 echo "Shit! ". "</Br>"; 7} 8 9 $ test = "OK"; 10 var_dump ($ test); 11 \ ModernPHP \ feature \ mingmingkongjian \ var_dump (); 12 13 // The namespace must start with the header, but a file can have many namespaces, And then there can be a sub-space 14 // The namespace of the vendor is the top-level namespace, used to identify the Brand 15 // to solve the naming conflict problem, of course, there should be more flexible usage 16 17 // a more practical point: import and alias 18 // import the class definition in another folder, directly use 19 require 'index. php '; 20 use a \ aaa; 21 $ daoru = new aaa; 22 $ daoru-> send (); 23 // use is import, then, set the lazy alias 24 // in use. In addition, the use function 25 // use func a \ call; 26 // \ a \ call () can be implemented after version 5.6 ();
Index. php
1 <?php 2 namespace a; 3 class aaa{ 4 public function send(){ 5 echo "ok"; 6 } 7 } 8 9 function call(){10 echo "func_use is successful.";11 }
Ii. Use Interfaces
Interface, I didn't understand it too well. After I understood it, it was awesome!
An interface that everyone can use as long as they comply with the interface rules.
The following is an interface Example for obtaining content. You can also write more modules based on this interface. (In this example, none of the getContent modules can be used... Cry)
<? Php /// Chapter2.P19 // Feature_Interface // namespace ModernPHP \ feature \ jiekou; class DocumentStore {protected $ data = []; public function addDocument (Documentable $ document) {// The $ key = $ document-> getID (); $ value = $ document-> getContent (); $ this-> data [$ key] = $ value;} public function getDocuments () {return $ this-> data ;}} interface Documentable {// defines an interface, to put it bluntly, it is to set rules. To use them elsewhere, you have to say public function getId (); Public function getContent ();} class HtmlDocument implements Documentable {// declare the interface to use; this is the protected $ url for obtaining the url content; public function _ construct ($ url) {$ this-> url = $ url;} public function getId () {return $ this-> url;} public function getContent () {$ ch = curl_init (); // here the curl is used to operate a library (equivalent to) on the url ). This command is used to enable a curl dialog, so the following is a dialog curl_setopt ($ ch, CURLOPT_URL, $ this-> url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ ch, CURLOPT_MAXREDIRS, 3); $ html = curl_exec ($ ch ); // run this command to execute the preceding dialog curl_close ($ ch); return $ html ;}}$ documentStore = new DocumentStore (); $ htmlDoc = new HtmlDocument ('HTTP: // www.baidu.com '); $ documentStore-> addDocument ($ htmlDoc); print_r ($ documentStore-> getDocuments ());
Another Module
1 class StreamDocument implements Documentable {// Streaming Media 2 protected $ resource; 3 protected $ buffer; // buffer size 4 5 public function _ construct ($ resource, $ buffer = 4096) {6 $ this-> resource = $ resource; 7 $ this-> buffer = $ buffer; 8} 9 10 public function getId () {11 return 'Resource -'. (int) $ this-> resource; 12} 13 14 public function getContent () {15 $ streamContent = ''; 16 rewind ($ this-> resource ); // The rewind () function returns the bit of the file pointer. Set 17 while (feof ($ this-> resource) === false) at the beginning of the inverted file {// feof () function to check whether it has reached the end of the file (eof ). 18 $ streamContent. = fread ($ this-> resource, $ this-> buffer); 19} 20 21 return $ streamContent; 22} 23}
Iii. Traits
Something strange...
In fact, it is for multi-inheritance or one to multiple different classes.
1 <? Php 2 // 3 // Chapter2.P23 4 // Feature_Trait 5 // character 6 // 7 8 // The aforementioned interface is for the same type of things and implements the same functions; 9 // here the traits are for different things, implementing the same function 10 11 // basic usage 12 trait traitName {13 public function testThis () {14 echo "This is how trait works. ". "<br/>"; 15} 16} 17 18 trait traitMore {19 public function testAgain () {20 echo "This is multiple use. ". "<br/>"; 21} 22} 23 24 class className {25 use traitName; 26 use traitMore; 27 28} 29 30 $ classMine = new className (); 31 $ classMine-> testThis (); 32 $ classMine-> testAgain ();
Iv. Generator
Directly Add code
1 <? Php 2 // 3 // Chapter2.P26 4 // Feature_Generator 5 // generator 6 // 7 8 // It is actually something that uses the yield statement in the function 9 // The advantage is to save memory usage 10 // The method is to dynamically allocate memory for cyclic operations 11 // typical use is to process csv data files 12 13 namespace ModernPHP \ feature \ shengchegnqi; 14 15 function getRows ($ file) {16 $ handle = fopen ($ file, 'rb'); 17 if ($ handle = false) {18 throw new Exception (); // throw error cause 19} 20 while (feof ($ handle) ===false) {21 yield fgetcsv ($ handle ); 22} 23 fclose ($ handle); 24} 25 26 foreach (getRows('data.csv ') as $ row) {27 print_r ($ row); 28 echo "<br/> "; 29} 30 // when the data file is large, the effect is particularly remarkable
5. Closure
Here closures are basically equivalent to anonymous functions.
1 <? Php 2 // 3 // Chapter2.P29 4 // Feature_ClosePatch 5 // closure or anonymous function 6 // 7 8 // treat the function as variable 9 // then it can be like a variable .. 10 // callback 11 12 namespace ModernPHP \ feature \ bibao; 13 $ var = function ($ name) {14 return sprintf ('Hello % s ', $ name); 15}; 16 17 echo $ var ('andy '); 18 19 // do callback 20 $ array = [2, 3, 4]; 21 $ num = array_map (function ($ number) {// array_map, which applies the function to each value in the array. Each value is multiplied by itself, return the 22 return $ number + 1; 23}, $ array); 24 print_r ($ num );
6. Additional status
I don't understand this...
(2) Standards
Some conventions of PHP-FIG;
--- Class name, hump type, ShitHappens
--- Method name, hump type, but lowercase letter, shitHappens
--- Four spaces are indented.
--- Do not write?> End symbol;
--- {Start another line;
--- The namespace must have spaces;
--- Attributes and methods in the class must have a visibility Declaration;
--- If and other control structures are followed by spaces;
1 <?php 2 // 3 //Chapter3.P44 4 //PHP-FIG puts PSRs 5 // 6 7 namespace ModernPHP\standard\realize; 8 9 use ModernPHP\feature\bibao;10 use ModernPHP\feature\fujiazhuangtai;11 12 class ShitHappens13 {14 public $a;15 16 public function suck()17 {18 if ($this->a===false){19 return true;20 }21 }22 }
----------------------
I will talk about things later. If you need something, I will try again.