mordenphp reading Notes (a)--run first, run tired and then go, mordenphp First run _php tutorial

Source: Internet
Author: User
Tags rewind traits

mordenphp reading Notes (a)--run first, run tired and go again, mordenphp run first.


---restore content starts---

Backstage a large pile of semi-finished products, or is almost impossible ...

This book is good, at least recommended by others, and then is relatively new things, learn which is not learning, the key is to see.

Today a network is not good, the code needed for scientific research is not, read to take notes.

This book basically will be 5.4 version after some of the new changes, write easy to understand, although I walk not Shunliu, run a run also fall not where to go, run tired I have plenty of opportunities to go ~ ~

(a) characteristics

First, the name space

A file of a class, using a namespace to facilitate mutual invocation;

1 //2 //namespace3 //4 namespace Modernphp\feature\mingmingkongjian;5 function Var_dump(){6     Echo"Shit!". "
";7 }8 9 $test= "OK";Ten Var_dump($test); One\modernphp\feature\mingmingkongjian\Var_dump(); A - //namespaces must be head-on, but there can be many namespaces in a file, and then you can have sub-spaces - //The namespace of the manufacturer is the topmost namespace, used to identify the brand the //To solve the problem of naming conflicts, of course, there should be more flexible usage now - - //A more practical point: Import and Alias - //Import a class definition under another folder, directly using the + require' index.php '; - Usea\aaa; + $daoru=NewAAA; A $daoru-send (); at //Use is an import, then set the most lazy alias in use - ///In addition, use function can be implemented after version 5.6 - //use func A\call; - //\a\call ();

index.php

 
 1 
   PHP 2namespace A; 3 class aaa{  4      Public function Send () {  5         Echo "OK"; 6     }  7}  8  9function call () {10      echo "Func_use is successful." ;  One}

Second, the use of interfaces

Interface, originally did not understand very much, understand after the simply, Ah!

An interface, as long as the interface to comply with the provisions, it can be used, that is the meaning.

Here is an example of an interface to get the content, and also write more modules based on this interface, (wherein, the module getcontent I basically do not ... Cry

 Php////chapter2.p19//feature_interface//namespace Modernphp\feature\jiekou;classdocumentstore{protected $data=[];  Public functionAdddocument (documentable$document){//This indicates that only the parameters of the interface can be used        $key=$document-GetID (); $value=$document-getcontent (); $this->data[$key]=$value; }         Public functiongetdocuments () {return $this-data; }}Interfacedocumentable{//define the interface, White is to set the rules, other places to use, you have to say     Public functiongetId ();  Public functiongetcontent ();}classHTMLDocumentImplementsdocumentable{//declare to use the interface; This is the content of the URL .    protected $url;  Public function__construct ($url){        $this->url=$url; }         Public functiongetId () {return $this-URL; }         Public functiongetcontent () {$ch=curl_init ();//Curl Here is a library that operates on a URL (equivalent). This command is to open a curl dialog, so these are all a conversationcurl_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);//by this command, the conversation was just done.Curl_close ($ch); return $html; }}$documentStore=NewDocumentstore ();$htmlDoc=NewHTMLDocument (' http://www.baidu.com ');$documentStore->adddocument ($htmlDoc);Print_r($documentStore->getdocuments ());

Another module

1 classStreamdocumentImplementsdocumentable{//Streaming Media2     protected $resource;3     protected $buffer;//Buffer size4     5      Public function__construct ($resource,$buffer=4096){6         $this-Resource=$resource;7         $this->buffer=$buffer;8     }9     Ten      Public functiongetId () { One         return' resource-'. (int)$this-Resource; A     } -      -      Public functiongetcontent () { the         $streamContent=''; -         Rewind($this-Resource);//The Rewind () function returns the position of the file pointer back to the beginning of the file -          while(feof($this-Resource)===false){//The feof () function detects if the end of file (EOF) has been reached.  -             $streamContent.=fread($this-Resource,$this-buffer); +         } -          +         return $streamContent; A     } at}

Third, traits

Strange things ...

It's just for multiple inheritance or a couple of different categories.

1 
 Php2 //3 //chapter2.p234 //feature_trait5 //Traits6 //7 8 //Front said interface, is for the same type of thing, to achieve the same function;9 //The traits here are for different things to achieve the same functionTen  One //Basic usage is as follows A Trait traitname{ -      Public functionTestthis () { -         Echo"This was how trait works."
"; the } - } - - Trait traitmore{ + Public functionTestagain () { - Echo"This was multiple use.". "
"; + } A } at - classclassname{ - UseTraitname; - UseTraitmore; - - } in - $classMine=NewclassName (); to $classMine-testthis (); + $classMine->testagain ();

Iv. Generators

Directly on the code

1 
 Php2 //3 //chapter2.p264 //feature_generator5 //Generator6 //7 8 //is actually something that uses the yield statement in a function9 //advantage of saving memory usageTen //method is to loop through dynamic allocation of memory One //Typical use is to process CSV class data files A  - namespace Modernphp\feature\shengchegnqi; -  the functionGetRows ($file){ -     $handle=fopen($file, ' RB '); -     if($handle===false){ -         Throw New Exception();//throw the wrong cause +     } -      while(feof($handle)===false) { +YieldFgetcsv($handle); A     } at     fclose($handle); - } -  - foreach(GetRows (' data.csv ') as $row){ -     Print_r($row); -     Echo"
"; in } - //the effect is especially noticeable when the data file is large

Five, closed package

The closure here basically equals the anonymous function

1 
 Php2 //3 //chapter2.p294 //feature_closepatch5 //closures or anonymous functions6 //7 8 //function as a variable9 //Then it can be used like a variable. Ten //callbacks used to do functions and methods One  A namespace Modernphp\feature\bibao; - $var=function($name){ -     return sprintf(' Hello%s ',$name); the }; -  - Echo $var(' Andy '); -  + //make a callback. - $array=[2,3,4]; + $num=Array_map(function($number){//Array_map, the function is scoped to each value in the array, each value is multiplied by itself, and an array with the new value is returned A     return $number+1; at},$array); - Print_r($num);

Vi.. Additional Status

This doesn't make sense.

(ii) standard

Some conventions of php-fig;

---class name, hump type, Shithappens

---method name, hump type, but first letter lowercase, shithappens

---indent unified to 4 spaces

---do not write?> end symbols;

---{another line;

---namespace should have a space;

The properties and methods in the---class must have a declaration of visibility;

---If the control structure is followed by a space;

1 
 Php2 //3 //CHAPTER3.P444 //php-fig puts PSRs5 //6 7 namespace Modernphp\standard\realize;8 9  UseModernphp\feature\bibao;Ten  UseModernphp\feature\fujiazhuangtai; One  A classShithappens - { -      Public $a; the      -      Public functionsuck () -     { -         if($this->a===false){ +             return true; -         } +     } A}

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

The back is all about the things that I need to write again.

http://www.bkjia.com/PHPjc/1121987.html www.bkjia.com true http://www.bkjia.com/PHPjc/1121987.html techarticle mordenphp Reading Notes (a)--run first, run tired and then go, mordenphp First run---restore content start---backstage a lot of semi-finished, or almost impossible ... This book is not ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.